Help: PCLEX, PCYACC and reserved words

"Garry Whitworth" <garryw@cyberus.com>
5 Dec 1997 01:20:54 -0500

          From comp.compilers

Related articles
Help: PCLEX, PCYACC and reserved words garryw@cyberus.com (Garry Whitworth) (1997-12-05)
Re: Help: PCLEX, PCYACC and reserved words fjh@murlibobo.cs.mu.OZ.AU (1997-12-07)
Re: Help: PCLEX, PCYACC and reserved words mark@research.techforce.nl (Mark Thiehatten) (1997-12-10)
Re: Help: PCLEX, PCYACC and reserved words clark@quarry.zk3.dec.com (Chris Clark USG) (1997-12-10)
| List of all articles for this month |
From: "Garry Whitworth" <garryw@cyberus.com>
Newsgroups: comp.compilers
Date: 5 Dec 1997 01:20:54 -0500
Organization: Compilers Central
Keywords: parse, design, question

Hi,


I'm fairly new(bie) to the compiler world and have been using Abraxas
PCLEX and PCYACC for the last month to build grammar files for a 4th
generation language currently on the market. If you are familiar with
4GL languages, they are extremely wordy (my .l file currenty has 400
reserved words for the report writer component of the language
alone). I have two questions which I would like to ask those of you
familiar with Lex and Yacc.


1st Question: In this 4GL language, a programmer can be lazy and enter in
PERC instead of PERCENT, CHAR instead of CHARACTER, NUM instead of NUMERIC,
and so on..... My current lex file has the following lines


NUMERIC { return NUMERIC; }
CHARACTER { return CHARACTER; }
PERCENT { return PERCENT; }


Is there an easy way for my lex file to say that PER, PERC, PERCE,
PERCEN, and PERCENT should all return PERCENT? CHAR, CHARA, CHARAC,
CHARACT, CHARACTE, and CHARACTER all return CHARACTER?


2nd Question: The language I am dealing with also allows the
programmer to declare a variable using one of its reserved word, so
they could in effect declare a variable called PERCENT, CHARACTER,
etc... Unforetunately, once the Lexer hits the reserved word
CHARACTER, it returns CHARACTER to the parser and throws the grammar
file off. Is there a way for me to tell the grammar file that a
reserved word is OK in certain instances (i.e., context
sensitive). For example, if my YACC rule is as follows:


declare_variable:
DECLARE id = PERCENT id
| DECLARE id = SUBTOTAL id
;


where DECLARE, PERCENT and SUBTOTAL are all reserved words in my Lex file,
how could I allow my grammar to handle


DECLARE percent = PERCENT order_total
DECLARE subtotal = PERCENT order_total
etc...


Where percent and subtotal are actually the names of the variables and not
the reserved words.


I appreciate any help I can get.


Thanks,


Garry Whitworth
[Yuck. For the partial name problem, I'd have the lexer recognize
them all as symbols, then in the semantic code look up the symbol in a
table of keywords using prefix matching. The keyword as symbol problem is
a lot harder. Sometimes you can get away with an ugly grammar that says


symbol | thiskeyword | thatkeyword | theotherkeyword


all over the place, or maybe you can identify the contexts well enough in
the parser to tell the lexer when it's expecting a keyword and when it
isn't. -John]




--


Post a followup to this message

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