[antlr-interest] How do I accept input ending with a newline *or* EOF?

Micha micha-1 at fantasymail.de
Fri Feb 4 00:30:53 PST 2011


Am 04.02.2011 04:21, schrieb chris king:
> Suppose I want to create a grammar to recognize integers with one integer
> per line. Additionally suppose I want a preprocessor such that I can wrap
> groups of integers in #if [expr] ... #endif where if expr has basic
> arithmetic operations. If expr evaluates to greater than 0 the enclosed
> lines are lexed else they are not. What would that grammar (or even just
> lexer) look like?
> 
> So for example, it should recognize this input and return an integer token
> 10 but not a token for 20. It should also ignore the comment.
> 
> 
> 10
> #if (2-1)*3-10
> 20 and just ignore this comment
> #endif
> 
> I cannot come up with a lexer that can lex the whole file without knowing if
> it's in an active #if\#endif block so I'm stuck before I can even begin my
> grammar.


I think it would be better to open a new thread for a new question :-)

The lexer itself isn't that suited to capture blockstructures, but if
you really want to do it, you can maintain yourself a stack of states,
onto which you push something like 'IN_BLOCK' after seeing the #if and
remove it after seeing the #endif  (if you want to nest the blocks)
You lex the tokens as normal, but when inside a block you return just a
NL or similar. You could use this to prescan the input ....
Maybe some book about compiler construction or DSL could help also (the
antlr dsl book is really helpful)

cheers,
 Michael




More information about the antlr-interest mailing list