Re: lex rules (Fergus Henderson)
1 Dec 2000 13:35:38 -0500

          From comp.compilers

Related articles
lex rules (Blake Stone) (2000-11-30)
Re: lex rules (2000-12-01)
Re: lex rules (Esmond Pitt) (2000-12-01)
| List of all articles for this month |

From: (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" <> 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

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 <> | "I have always known that the pursuit
                                                                        | of excellence is a lethal habit"
WWW: <> | -- 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.