我正在尝试生成与配方成分相关的解析器。我注意到解析器处理标记的顺序似乎遵循 jison 文件中标记的行项顺序,而不是 EBNF 语法中定义的内容。
例如,解析6 tablespoons unsalted butter, cut into 1-inch pieces
产生:
Error: Parse error on line 1:
6 tablespoons unsalted
--^
Expecting 'UNIT_NAME', 'NUMBER', 'SLASH', got 'WORD'
我希望语法在它吃掉之前看到UNIT_NAME
哪个是. 这里正确的语法方法是什么?我一直在使用交互式 Jison 解析器来验证语法状态,到目前为止还没有看到任何问题。tablespoons
WORD
吉森·格拉默
%lex
%options flex case-insensitive
UnitName [teaspoons|teaspoon|tablespoons|tablespoon|fluid ounces|fluid ounce|ounces|ounce|cups|cup|pints|pint|quarts|quart|gallons|gallon|pounds|pound|milliliters|milliliter|deciliters|deciliter|liters|liter]\b
Word \w+\b
NUMBER [1-9][0-9]+|[0-9]
CHAR [a-zA-Z0-9_-]
%%
\s+ /* skip whitespace */
{NUMBER} return 'NUMBER'
{UnitName} return "UNIT_NAME";
{Word} return 'WORD'
{CHAR} return 'CHAR'
"/" return "SLASH";
"-" return "HYPHEN"
"," return "COMMA";
<<EOF>> return 'EOF';
/lex
/* enable EBNF grammar syntax */
%ebnf
/* language grammar */
%start ingredient
%%
ingredient
: ingredient_format
{ return $1; }
;
ingredient_format
: unit_count UNIT_NAME ingredient_name COMMA ingredient_info EOF
{ $$ = {'count': $1, 'unit': $2, 'item': $3, info: $5}; }
| unit_count UNIT_NAME ingredient_name EOF
{ $$ = {'count': $1, 'unit': $2, 'item': $3, info: null}; }
;
unit_count
: NUMBER
{ $$ = parseInt($1); }
| NUMBER SLASH NUMBER
{ $$ = parseInt($1) / parseInt($3); }
| NUMBER NUMBER SLASH NUMBER
{ $$ = parseInt($1) + (parseInt($2) / parseInt($4)); }
;
ingredient_name
: WORD+
{ $$ = $1; }
;
ingredient_info
: ""
{ $$ = ''; }
| WORD+
{ $$ = $1; }
;
要旨
我创建了一个带有一些文本字符串和一个简单的解析器来测试: https ://gist.github.com/aphexddb/ddc83d57c7f1c1b96458