【发布时间】:2015-10-24 18:10:06
【问题描述】:
我开始为单一语言编写一个轻型解释器来管理图形。我正在使用 flex 和 bison,但在定义语法时遇到了一些问题。
目前,我只想解析三个单独的命令:
load "file-name"save "file-name"exit
这是yacc中的语法:
%{
# include <iostream>
using namespace std;
int yylex(void);
void yyerror(char const *);
%}
%token LOAD SAVE RIF COD EXIT STRCONST VARNAME
%%
input: line
;
line: cmd_unit '\n'
{
cout << "PARSED LINE with EOL" << endl;
}
| cmd_unit
{
cout << "PARSED LINE without EOL" << endl;
}
;
cmd_unit: LOAD STRCONST
{
cout << "PARSED LOAD" << endl;
}
| SAVE STRCONST
{
cout << "PARSED SAVE" << endl;
}
| EXIT { }
;
%%
现在这是词法分析器和 lex 中非常简单的 repl:
%{
# include <net-parser.H>
# include "test.tab.h"
YYSTYPE netyylval;
size_t curr_lineno = 0;
# define yylval netyylval
/* Max size of string constants */
# define MAX_STR_CONST 4097
# define MAX_CWD_SIZE 4097
# define YY_NO_UNPUT /* keep g++ happy */
/* define YY_INPUT so we read thorugh readline */
/* # undef YY_INPUT */
/* # define YY_INPUT(buf, result, max_size) result = get_input(buf, max_size); */
char string_buf[MAX_STR_CONST]; /* to assemble string constants */
char *string_buf_ptr = string_buf;
/*
* Add Your own definitions here
*/
bool string_error = false;
inline bool put_char_in_buf(char c)
{
if (string_buf_ptr == &string_buf[MAX_STR_CONST - 1])
{
yylval.error_msg = "String constant too long";
string_error = true;
return false;
}
*string_buf_ptr++ = c;
return true;
}
%}
%x STRING
/*
* Define names for regular expressions here.
*/
/* Keywords */
LOAD [lL][oO][aA][dD]
SAVE [sS][aA][vV][eE]
RIF [rR][iI][fF]
COD [cC][oO][dD]
EXIT [eE][xX][iI][tT]
DIGIT [0-9]
UPPER_LETTER [A-Z]
LOWER_LETTER [a-z]
ANY_LETTER ({UPPER_LETTER}|{LOWER_LETTER})
SPACE [ \f\r\t\v]
NEWLINE \n
INTEGER {DIGIT}+
ID {INTEGER}
VARNAME {ANY_LETTER}([_\.-]|{ANY_LETTER}|{DIGIT})*
%%
{SPACE} /* Ignore spaces */
{NEWLINE} { ++curr_lineno; return NEWLINE; }
/*
* Keywords are case-insensitive except for the values true and false,
* which must begin with a lower-case letter.
*/
{LOAD} return LOAD;
{SAVE} return SAVE;
{RIF} return RIF;
{COD} return COD;
{EXIT} return EXIT;
/*
* The single-characters tokens
*/
[=;] return *yytext;
/*
* String constants (C syntax)
* Escape sequence \c is accepted for all characters c. Except for
* \n \t \b \f, the result is c.
*
*/
\" { /* start of string */
string_buf_ptr = &string_buf[0];
string_error = false;
BEGIN(STRING);
}
<STRING>[^\\\"\n\0] {
if (not put_char_in_buf(*yytext))
return ERROR;
}
<STRING>\\\n { // escaped string
if (not put_char_in_buf('\n'))
return ERROR;
++curr_lineno;
}
<STRING>\\n {
if (not put_char_in_buf('\n'))
return ERROR;
}
<STRING>\\t {
if (not put_char_in_buf('\t'))
return ERROR;
}
<STRING>\\b {
if (not put_char_in_buf('\b'))
return ERROR;
}
<STRING>\\f {
if (not put_char_in_buf('\f'))
return ERROR;
}
<STRING>\\\0 {
yylval.error_msg = "String contains escaped null character.";
string_error = true;
return ERROR;
}
<STRING>{NEWLINE} {
BEGIN(INITIAL);
++curr_lineno;
yylval.error_msg = "Unterminated string constant";
return ERROR;
}
<STRING>\" { /* end of string */
*string_buf_ptr = '\0';
BEGIN(INITIAL);
if (not string_error)
{
yylval.symbol = strdup(string_buf); // TODO: ojo con este memory leak
return STRCONST;
}
}
<STRING>\\[^\n\0ntbf] {
if (not put_char_in_buf(yytext[1]))
return ERROR;
}
<STRING>'\0' {
yylval.error_msg = "String contains escaped null character.";
string_error = true;
return ERROR;
}
<STRING><<EOF>> {
yylval.error_msg = "EOF in string constant";
BEGIN(INITIAL);
return ERROR;
}
{ID} { // matches integer constant
yylval.symbol = yytext;
return ID;
}
{VARNAME} {
yylval.symbol = yytext;
return VARNAME;
}
. {
cout << "LEX ERROR" << endl;
yylval.error_msg = yytext;
return ERROR;
}
%%
int yywrap()
{
return 1;
}
extern int yyparse();
string get_prompt(size_t i)
{
stringstream s;
s << i << " > ";
return s.str();
}
int main()
{
for (size_t i = 0; true; ++i)
{
string prompt = get_prompt(i);
char * line = readline(prompt.c_str());
if (line == nullptr)
break;
YY_BUFFER_STATE bp = yy_scan_string(line);
yy_switch_to_buffer(bp);
free(line);
int status = yyparse();
cout << "PARSING STATUS = " << status << endl;
yy_delete_buffer(bp);
}
}
如您所见,词法分析器的很大一部分专门用于识别字符串常量。我不知道这个词法分析器是否完美和优雅,但我可以说我对它进行了深入测试并且它可以工作。
现在,当程序被调用时,这是一个跟踪:
0 > load "name"
ERROR syntax error
PARSING STATUS = 1
1 >
也就是说,肯定是指定错误的语法无法识别规则
cmd_unit: LOAD STRCONST
好吧,虽然可以肯定我没有主宰语法世界,但我花了一些重要的时间来理解这个小而简单的规范,但我仍然无法理解为什么它无法解析一个非常单一的规则。我几乎可以肯定这是一个愚蠢的错误,但我知道是哪个。
所以,我真的非常感谢任何帮助。
【问题讨论】:
标签: c++ parsing bison readline yacc