Thread: [Pyparsing] Multiline expressions
Brought to you by:
ptmcg
From: Roberto A. <ra...@kd...> - 2006-10-02 17:54:32
|
Hello, I am a newbie to pyparsing (and parsers in general) and things are= =20 going good, except... How can I parse something like this if (expression) { command command command } else { command command } My problem is that apparently all expressions match only in a single line. =46or example: >>> from pyparsing import * >>> w=3DWord(alphanums) >>> lines=3DdelimitedList(w,"\n") >>> lines.parseString("""111111 =2E.. 222222 =2E.. 333333 =2E.. """ =2E.. ) (['111111'], {}) >>> Rather than the three items I expected. So, I can't match for a block like "{"+whatever+"}" I must be missing something very obvious here :-) =2D-=20 =A0("\''/").__..-''"`-. . =A0 =A0 =A0 =A0 Roberto Alsina =A0`9_ 9 =A0) =A0 `-. ( =A0 =A0).`-._.`) =A0r...@kd... =A0(_Y_.)' ._ =A0 ) `._`. =A0" -.-' =A0 KDE Developer (MFCH) =A0 _..`-'_..-_/ /-'_.' (l)-'' ((i).' ((!.' =A0 Buenos Aires - Argentina =46eynman's problem solving algorithm: write down the problem-> think very hard -> write down the answer. |
From: Paul M. <pa...@al...> - 2006-10-02 19:02:40
|
Unless you disable whitespace skipping, do not use delimitedList to read lists of items separated only by whitespace. This is where pyparsing is different from regular expressions. Pyparsing will treat whitespace as = a terminating boundary for words, but will implicitly skip over it for = parsing purposes. The delimitedList method returns an expression for things = like comma-delimited lists, or colon-delimited lists. Lists of items = separated only by whitespace can be parsed using a simple OneOrMore. This whitespace-skipping behavior makes it tricky to parse grammars with significant whitespace (like Python), but in the grammar you list = (assuming that commands have some kind of terminating character, like a ';'), whitespace truly is irrelevant. So: if (expression) { do_x; do_y; do_z; } is the same as:=20 if (expression) { do_x; do_y; do_z; } And you could implement this as: if_statement =3D Keyword("if") + \ "(" + conditional_expression + ")" + \ ( command | "{" + OneOrMore(command) + "}" ) assuming that ';' has been included in the definition of command. If you are parsing Pascal, the ';' is actually a statement separater, = not a terminator, so you would parse: if (expression) { do_x; do_y; do_z } using: if_statement =3D Keyword("if") + \ "(" + conditional_expression + ")" + \ ( command | "{" + delimitedList(command,';') + "}" ) and leave the ';' out of the definition of command. I would also recommend enclosing command within a Group so as to = maintain some idea of the code hierarchy. Lastly, assuming that an if_statement is a possible command, then I = assume you have previously defined command as a Forward(), and some subsequent = line would have something like: command << ( assignment_statement | if_statement | while_statement | switch_statement | function_call ) -- Paul > -----Original Message----- > From: pyp...@li...=20 > [mailto:pyp...@li...] On=20 > Behalf Of Roberto Alsina > Sent: Monday, October 02, 2006 12:54 PM > To: pyp...@li... > Subject: [Pyparsing] Multiline expressions >=20 > Hello, I am a newbie to pyparsing (and parsers in general)=20 > and things are going good, except... >=20 > How can I parse something like this >=20 > if (expression) { > command > command > command > } else { > command > command > } >=20 > My problem is that apparently all expressions match only in a=20 > single line. >=20 > For example: >=20 > >>> from pyparsing import * > >>> w=3DWord(alphanums) > >>> lines=3DdelimitedList(w,"\n") > >>> lines.parseString("""111111 > ... 222222 > ... 333333 > ... """ > ... ) > (['111111'], {}) > >>> >=20 > Rather than the three items I expected. >=20 > So, I can't match for a block like "{"+whatever+"}" >=20 > I must be missing something very obvious here :-) >=20 > -- > =A0("\''/").__..-''"`-. . =A0 =A0 =A0 =A0 Roberto Alsina > =A0`9_ 9 =A0) =A0 `-. ( =A0 =A0).`-._.`) =A0r...@kd... > =A0(_Y_.)' ._ =A0 ) `._`. =A0" -.-' =A0 KDE Developer (MFCH) > =A0 _..`-'_..-_/ /-'_.' > (l)-'' ((i).' ((!.' =A0 Buenos Aires - Argentina Feynman's=20 > problem solving algorithm: write down the problem-> think=20 > very hard -> write down the answer. >=20 > -------------------------------------------------------------- > ----------- > Take Surveys. Earn Cash. Influence the Future of IT Join=20 > SourceForge.net's Techsay panel and you'll get the chance to=20 > share your opinions on IT & business topics through brief=20 > surveys -- and earn cash=20 > http://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge &CID=3DDEVDEV > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users >=20 >=20 |