For Programmers: Free Programming Magazines  


Home > Archive > Compilers > March 2007 > ANTLR and LEX states









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ANTLR and LEX states
Amal

2007-03-29, 4:27 am

How do you rewrite/convert lex grammars that include states to ANTLR?
There is a simple example in the ANTLR documentation, but it is not
clear to me how to convert a grammar similar to the following.

This is a snippet from VCD parser from Maarten Ditzel.

%x text

%{
%}

s [[:space:]]

%%

<INITIAL>{s}+ { /* skip whitespace */ }

\$comment { BEGIN(text); return TK_COMMENT; }
\$version { BEGIN(text); return TK_VERSION; }

<text>\$end { BEGIN(INITIAL); return TK_END; }
<text>. { yylineno--; yymore(); }
<text>\n { yylineno--; yymore(); }
<text>./\$end { sval = yytext; return TK_TEXT; }
<text>\n/\$end { sval = yytext; return TK_TEXT; }

Basically I am trying to parse something like:

$comment
any character here
including CR/LF
$end

$version
any character here
including CR/LF
$end

There are a lot more states in this grammar, but it would be
appreciated if anyone has hints for converting this snippet of the
grammar.

Thanks,
-- Amal

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com