How to decide what to put in the lexer and the grammar respectively?

Peng Yu <[email protected]>
Newsgroups gmane.comp.parsers.bison.general
Message-ID <CABrM6wm87acVsazqJHWAeQ5NAuXBFqsKOs4HtjJCpV-nazzmnw@mail.gmail.com>
Hi,

The more I study flex/bison, the more confused I got about when to use
what actions and what to put in lexer and what to put in grammar.

For example, for an assignment,

x=10

it can be processed by the lexer,

[[:alpha:]_][[:alnum:]_]=[[:digit:]+]  { /* parse yytext to get the
name and value, then do the assignment */ }

or use these lex rules

%x ASSIGNMENT
%%
[[:alpha:]_][[:alnum:]_]=  { BEGIN(ASSIGNMENT); /*save varaible name*/ }
<ASSIGNMENT>[[:digit:]]+ {
/* get the value and assign to the saved variable name*/
  BEGIN(INITIAL);
}

Alternatively, the lexer can just do

[[:alpha:]_][[:alnum:]_]  { /*save name somewhere for bison use */
return TOK_NAME; }
=                                  { return '='; }
[[:digit:]+]                     { /*save value somwhere for bison
use*/ return TOK_VALUE; }

Then, bison grammar can be something like this.

assignment: TOK_NAME '=' TOK_VALUE { /*do the assignment */}

There just seem to be too many possible ways of implementations. Could
anybody let me know how to decide which implementation to use in
practical scenarios?

-- 
Regards,
Peng

_______________________________________________
[email protected] https://lists.gnu.org/mailman/listinfo/help-bison
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.