help-bison
[Top][All Lists]
Advanced

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

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


From: Akim Demaille
Subject: Re: How to decide what to put in the lexer and the grammar respectively?
Date: Sun, 17 Feb 2019 15:44:40 +0100

Hi!

> Le 17 févr. 2019 à 14:08, Peng Yu <address@hidden> 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!


reply via email to

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