javascript - Jison: Getting parsed token instead of what is defined in Grammar -
i attempting generate parser related recipe ingredients. noticing order parser handles tokens seems follow token's line-item order in jison file, vs. whats defined in ebnf grammar.
for example, parsing 6 tablespoons unsalted butter, cut 1-inch pieces
yields:
error: parse error on line 1: 6 tablespoons unsalted --^ expecting 'unit_name', 'number', 'slash', got 'word'
i expect grammar see unit_name
tablespoons
before eats word
. right grammar approach here? have been using interactive jison parser validate grammar states , didnt see gotchas far.
jison grammer
%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; } ;
gist
i created text strings , simple parser test: https://gist.github.com/aphexddb/ddc83d57c7f1c1b96458
Comments
Post a Comment