[antlr-interest] Multi-level format parsing

Loring Craymer craymer at warpiv.com
Wed May 10 18:56:15 PDT 2006


My impression is that what you are describing is not a multi-level grammar,
but more a problem with differentiating levels of quoting.  If you identify
start and end quotes by

 

{ int quoteLevel = 0; }

QUOTE 

{ int temp = 0; }

:

 ( ‘”’  { temp++; } )+

{           if (temp == quoteLevel)

                        $setType(END_QUOTE);

            else if (temp == quoteLevel + 1)

                        quoteLevel++;

            else

                        // error!!!

}

;

/* END_QUOTE is defined in the “tokens” section */

And the parser recognizes

 

quoted_expr :

            QUOTE expr END_QUOTE

            ;

 

Then you don’t have to worry about stripping quotes and repeating the lex
and parse steps.  It gets a bit messier if you recognize different tokens
inside of quotes than you do outside, but not more difficult.

 

--Loring

 

  _____  

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Spálený Ivo
Sent: Wednesday, May 10, 2006 2:55 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Multi-level format parsing

 

Hi,

 

How can I create a grammar for multi-level parsing in ANTLR?

 

For an example – encoded literal, quote escaped. I need to read and parse
code in its value too.

 

CodeParam = "Var2 = ""text"""

 

where CodeParam includes another code:

 

Var = "text"

 

So the resulting tree is something like this:

(to avoid semigraphics experiments, I used XML-like tree)

 

<nodes>

=

<leftchild>

            CodeParam

</leftchild>

<rightchild>

            EncodedString

<leftchild>

=

<leftchild>

            Var

</leftchild>

<rightchild>

            „text“

</rightchild>

</leftchild>

</rightchild>

</nodes>

 

I can define a grammar rule:

 

stmt_assign : LITERAL EQU QUOTEDLITERAL ;

 

And later, after 1st level parsing, I remove quotes from QUOTEDLITERAL and
parse it again. 

 

But I would like to add this code directly to ANTLR grammar.  

 

Thank you for any suggestions,

 

Ivo Spaleny

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060510/ef5008b4/attachment-0001.html


More information about the antlr-interest mailing list