[antlr-interest] Matching Last Line in ANTLR?

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Tue Aug 18 06:50:51 PDT 2009


Dave Dutcher wrote:
>> -----Original Message-----
>> From: consiliens at gmail.com
>>
>> The last line, b., doesn't match the MC_INCORRECT token 
>> because there's no newline after it. Is there an easy way to 
>> match this in ANTLR?
> 
> 
> Do you need the newline in the text of the token?  You could try removing
> fragment from the NEWLINE rule and then setting it to the hidden channel.
> Then have your lexer rules match everything except the new line characters.

That would remove the requirement that each thingy be on a separate 
line. You could consider various options. For one thing, I'm not sure 
you *really* want those to be huge lexer rules, rather than moving it up 
to the parsing stage. Then you could make newlines a separate rule but 
*don't* make them hidden, and account for them at parse-time.

Otherwise, there's a non-antlr solution. Pre-cache the file (ie load it 
to a string buffer), check if the last character is a \n, if it isn't, 
append one. Then use a relevant reader to pass the buffer to the lexer, 
rather than the file.

-- 
Sam Barnett-Cormack


More information about the antlr-interest mailing list