我在lex和yacc中真的很新,我想編寫一個非常簡單的程序,它詢問字符串的輸入,將它存儲在變量中,並檢查是否一次插入相同的值再次。比方說:我法文件lex yacc比較兩個字符串
input1 = 'abc'
input2 = 'def'
input3 = 'ghi'
input4 = 'def'
STOP input2 equals input4
部分:我YACC文件
%{
# include <stdio.h>
# include <ctype.h>
# include <string.h>
%}
%union {
char* lexeme;
}
%token ID
%%
所有的投入應該在ID標記內匹配的
%option noyywrap
%{
#include <stdlib.h>
#include <string.h>
%}
alpha [a-zA-Z]
%%
{alpha}* return ID;
一部分。
您需要保留一個變量及其內容的表格。這不是Yacc會爲你做的事情,但你必須編寫正常的C(或其他語言)代碼才能做到。 Yacc將幫助您檢查輸入的格式,並在輸入中找到新分配時調用您的代碼。 –
[Flex/Lex - 我如何知道變量是否被聲明]可能的重複(http://stackoverflow.com/questions/27284546/flex-lex-how-do-i-know-if-a-variable-被宣佈) –