سؤال

The below code snippet can be found on: http://zaach.github.io/jison/demos/calc/, and also the jison documentation page. After reading the jison, lex, and flex documentation - I still don't fully understand the %lex and /lex syntax. Is it specific to the jison scanner generator? Meaning is its only function to provide the json output later shown in the documentation? I only ask because the jison documentation doesn't explicitly explain its purpose, and the flex/lex rules don't seem to allow for such syntax.

/* description: Parses end executes mathematical expressions. */

/* lexical grammar */
%lex

%%
\s+                   /* skip whitespace */
[0-9]+("."[0-9]+)?\b  return 'NUMBER';
"*"                   return '*';
"/"                   return '/';
"-"                   return '-';
"+"                   return '+';
"^"                   return '^';
"("                   return '(';
")"                   return ')';
"PI"                  return 'PI';
"E"                   return 'E';
<<EOF>>               return 'EOF';

/lex
هل كانت مفيدة؟

المحلول

The %lex and /lex markers only delimit the section of the grammar that pertain to the scanner generator. The %lex marker marks the start and /lex marks the end.

When using bison and flex you'd put the lexer's definitions (the "scanner generator" in Jison parlance) in a .l file and the grammar in a .y file. I do not think there exist a way using bison and flex (or yacc/lex) to combine the two files. (It has been a very long while since I've used bison and flex so it is not impossible that there's something I don't know.)

The fact is that although Jison takes inspiration from bison and flex, it is really an independent tool. So it does include features that have no equivalent in bison or flex.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top