Related articles |
---|
kimwitu lex and yacc... abate@gutturnio.students.cs.unibo.it (2000-05-01) |
Re: kimwitu lex and yacc... boehme@informatik.hu-berlin.de (Harald Boehme) (2000-05-04) |
Re: kimwitu lex and yacc... belinfan@cs.utwente.nl (2000-05-10) |
From: | belinfan@cs.utwente.nl () |
Newsgroups: | comp.compilers |
Date: | 10 May 2000 02:50:50 -0400 |
Organization: | Univ. of Twente, Dept. of Computer Science, Tele Informatics group |
References: | 00-05-009 00-05-023 |
Keywords: | tools, parse |
Pietro Abate wrote:
|> > I want to use kimwitu to make a simple parser for a pascal-like
|> > language, but I'm not able to find any example to do this... I've
|> > seen the calculator example in the kimwitu distribution but I've not
|> > realy understood which are the realtions between kimwitu, lex and
|> > yacc. Does anybody could help me with an realy little example that
Just trying to point out which is which in the calculator example,
at <URL:http://fmt.cs.utwente.nl/kimwitu/tacas97.demo/card03.html>
Harald Boehme <boehme@informatik.hu-berlin.de> writes:
|> So you have to write down the syntax of the lang. in terms of an lex-
|> and yacc-file. The rules in the yacc-file should than extended to
|> construct the phyla (subtree) and pass it up to the upper rule via the
|> yacc value stack ($$-notation).
This is shown/done in
<URL:http://fmt.cs.utwente.nl/kimwitu/tacas97.demo/card08.html>
|> For this purpose kimwitu and kimwitu++
|> provide an YYSTYPE definition to use in yacc.
In the calculator example we use the following kimwitu input
to define our phyla:
expr: Const( int ) /* int is predefined */
| Plus( expr expr )
| Times( expr expr )
;
(at <URL:http://fmt.cs.utwente.nl/kimwitu/tacas97.demo/card04.html>)
>From this input, among other things, the C type 'expr' and the C
function Plus (prototype: expr Plus(expr,expr) ) are generated.
In addition a union type YYSTYPE is generated, something like:
union YYSTYPE {
int yt_int;
casestring yt_casestring;
expr yt_expr;
};
This YYSTYPE is used in the following declarations in the yacc input:
%token <yt_int> T_INT
%type <yt_expr> Y_expr Y_term Y_factor
These declarations indicate that the things put on the yacc value stack
for Y_expr, Y_term and Y_factor are of phylum 'expr', and that the
value associated with token T_INT is of type int.
In the parse rules the functions generated from the kimwitu input
are used to construct the expr subtree, for example, below in the
rule for '+' we use the Plus function to construct a tree containing
the subtree of Y_expr ($1) and the subtree of Y_term ($3).
Y_expr
: Y_term { $$ = $1; }
| Y_expr '+' Y_term { $$ = Plus( $1, $3 ); }
;
The following rule in the lex input file at
<URL:http://fmt.cs.utwente.nl/kimwitu/tacas97.demo/card09.html>
shows how the integer value for token T_INT is constructed:
[0-9]+ { yylval.yt_int = atoi(yytext);
return T_INT; }
when that rule matches, the string of the input that represents
the match is in yytext. yylval is a variable of type YYSTYPE that
is used to pass the value associated with a token from the scanner
to the parser, so in the first line above we use atoi to compute
the integer value from the matched input string, and assign it
as an integer to yylval, after which we return token T_INT.
I hope this helps a bit - I'm aware of the fact that our kimwitu
documentation assumes prioe knowledge of yacc and lex, and only
explains how kimwitu can be used with them, without explaining yacc
and lex themselves, but I'm afraid there is not much I can do about
that. However, please feel free to contact me directly if you have any
more (detailed) questions regarding kimwitu.
Axel.
--
<Axel.Belinfante@cs.utwente.nl> <URL:http://www.cs.utwente.nl/~belinfan/>
University of Twente, Dept. of Computer Science, Formal Methods & Tools Group
P.O. Box 217; NL-7500 AE Enschede. Phone: +31 53 4893774; Fax: +31 53 4893247
Return to the
comp.compilers page.
Search the
comp.compilers archives again.