Re: Suggestion for dynamic grammar/parser - pls advise

Detlef Meyer-Eltz <Meyer-Eltz@t-online.de>
1 Apr 2007 08:58:22 -0400

          From comp.compilers

Related articles
[4 earlier articles]
Re: Suggestion for dynamic grammar/parser - pls advise mefrill@yandex.ru (mefrill) (2007-03-30)
Re: Suggestion for dynamic grammar/parser - pls advise nicola.musatti@gmail.com (Nicola Musatti) (2007-03-30)
Re: Suggestion for dynamic grammar/parser - pls advise jsassojr@nycap.rr.com (John Sasso) (2007-03-30)
Re: Suggestion for dynamic grammar/parser - pls advise jsassojr@nycap.rr.com (John Sasso) (2007-03-30)
Re: Suggestion for dynamic grammar/parser - pls advise DrDiettrich1@aol.com (Hans-Peter Diettrich) (2007-04-01)
Re: Suggestion for dynamic grammar/parser - pls advise Meyer-Eltz@t-online.de (Detlef Meyer-Eltz) (2007-04-01)
Re: Suggestion for dynamic grammar/parser - pls advise Meyer-Eltz@t-online.de (Detlef Meyer-Eltz) (2007-04-01)
| List of all articles for this month |

From: Detlef Meyer-Eltz <Meyer-Eltz@t-online.de>
Newsgroups: comp.compilers
Date: 1 Apr 2007 08:58:22 -0400
Organization: Compilers Central
References: 07-04-008
Keywords: parse

I just have sent my mail, as an improvement occurs to me. Instead of
testing the whole grammar you could write a special production to test
for the version only. For your original example this would look like:




GetVersion(int& xiVersion) ::=


SKIP? "set" "disk"
(
    SKIP "interface" {{ xiVersion = 2; }}
    (
        SKIP "map" {{ xiVersion = 3; }}
    )?
)?




SKIP is a special TextTransformer symbol, by which you can jump to the
token following on SKIP. In your example


set disk id 1 name "system" nparts 4 interface SCSI channel 2 \
map {
1 1032 8289508 "root",
1033 1162 1044225 "swap",
163 3120 15727635 "var",
3121 12161 72621832 "export"
}


SKIP? "set" "disk" would recognize the text up to "set disk"
SKIP "interface" would recognize the text up to "interface" and
                                                set xiVersion to 2
SKIP "map" would recognize the text up to "map" and set
                                                xiVersion to 3


If "set", "interface" or "map" can occur in later positions of the
script too, this production has to be refined.
"GetVersion" is called as a sub-parser in the main parser as follows:


{{
int iVersion = 0;
GetVersion(xState.str(-2), iVersion);
}}
IF(iVersion == 1)
G1
ELSE IF( iVersion == 2 )
G2
ELSE
...
END END END


by the parameter "xState.str(-2)" you pass the whole text of your
script to the sub-parser. This parameter is not in the definition of
GetVersion. When GetVersion ist called with an additional first string
parameter from the interpreter, an overloaded version of this production
is executed as sub-parser.


I didn't test the code above, but it should work. If you like, you can
try it


http://www.texttransformer.com


You only have to run the intaller. Than you can put the code above in
two production scripts and execute it in the IDE. For a first test you
can write


IF(iVersion == 1)
{{out << "Version 1"; }}
ELSE IF( iVersion == 2 )
{{out << "Version 2"; }}
END END


instead of writing the whole grammars G1 and G2. If there are any
difficulties, please contact me.




Detlef


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.