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 List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
