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

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.general
Message-ID <[email protected]>
Hi!

> Le 17 févr. 2019 à 14:08, Peng Yu <[email protected]> a écrit :
> 
> 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.

Usually it's quite clear: build words from letters in the scanner,
build sentences from words in the parser.  By "words", I mean
numbers, identifiers, keywords, strings, operators, etc.

> 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 */ }

If you're language is really simple and you can live with that,
then you can.  If your language has a rich structure, then it would
be silly.

Note that currently have don't support white spaces (including
\n), you don't support comment (in C, I can write "x /* var */
= /* value */ 42), and then you will have to traverse _again_
your "assignment" to find the lhs and rhs.

Clearly, define what is an identifier in the scanner, then
define their uses in the parser.


> There just seem to be too many possible ways of implementations.

As if often the case in computer science :)  But here, there is
a well established and well know best practice.

Cheers!
_______________________________________________
[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.