help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Peng Yu
Subject: How to decide what to put in the lexer and the grammar respectively?
Date: Sun, 17 Feb 2019 07:08:49 -0600

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]