Re: lex rules

fjh@cs.mu.OZ.AU (Fergus Henderson)
1 Dec 2000 13:35:38 -0500

          From comp.compilers

Related articles
lex rules brian@NOSPAMibc.com.au (Blake Stone) (2000-11-30)
Re: lex rules fjh@cs.mu.OZ.AU (2000-12-01)
Re: lex rules esmond.pitt@bigpond.com (Esmond Pitt) (2000-12-01)
| List of all articles for this month |

From: fjh@cs.mu.OZ.AU (Fergus Henderson)
Newsgroups: comp.compilers
Date: 1 Dec 2000 13:35:38 -0500
Organization: Computer Science, University of Melbourne
References: 00-11-176
Keywords: lex
Posted-Date: 01 Dec 2000 13:35:37 EST

"Blake Stone" <brian@NOSPAMibc.com.au> writes:
>I have some lex code below:
...
>VersionInfo {ws}"version"{eq}({dq}|{sq}){VersionNum}({dq}|{sq})
>EncodingDecl {ws}"encoding"{eq}({dq}|{sq}){EncName}({dq}|{sq})
>StandaloneDecl {ws}"standalone"{eq}({dq}|{sq})("yes"|"no")({dq}|{sq})
>XmlDecl "<?xml"{VersionInfo}{EncodingDecl}?{StandaloneDecl}?{ws}?"?>"
>
>%%
>{XmlDecl} { return XMLDECL; }
>
>Note that the EncodingDecl and StandaloneDecl should be optional. Yet, when
>I have <?xml version="1.0" ?> as input it does not match {XmlDecl}. If I
>remove{EncodingDecl} and {StandaloneDecl} then it works fine (or if I expand
>the input to use them). Why is this and how do I fix it?


This is because you didn't use parentheses in the appropriate places.
The fix is to add parentheses around the body of each of your macro
definitions.


To elaborate, `{...}' in lex is macro expansion, not subroutine call.
`?' applies only to the single item that immediately precedes it,
after macro expansion. If the macro foo is defined as `abc', then the
regexp `{foo}?' is the same as `abc?', not `(abc)?', and so matches
`ab' and `abc', rather than matching the empty string and `abc'.


The problem here is similar to the very common C preprocessor bug
of defining macros with definitions such as


#define sum(x, y) x + y


rather than


#define sum(x, y) ((x) + (y))


--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
                                                                        | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.


Post a followup to this message

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