[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [Chicken-users] LALR parser examples
From: |
Dominique Boucher |
Subject: |
RE: [Chicken-users] LALR parser examples |
Date: |
Mon, 23 Jan 2006 14:18:25 -0500 |
Hi Joel!
> I'm looking for LALR (egg) parser examples. Can you share any? An
> example of using a lexer like Silex (egg) with LALR would be most
> helpful.
See below:
> (require 'lalr)
>
> (define parser
> (lalr-parser
> ;; terminal symbols
> (ABOVE AGO ALERT AND ARRAY ARRAY-NUMERIC
> <= < >= > <> .. - + * / = [ (. .) ] ( ) . , ; : @ ^ )
> ...
>
> How do I quote the parens and brackets above and use them later? Is
> that even possible without renaming them to L-BRACKET, etc.?
Terminals can only be denoted by Scheme symbols. So yes, you'll have to
rename all special characters to LBRACKET, RBRACKET, SEMICOLON, etc. A Silex
lexer definition that would interface with parsers generated by LALR
would thus look like:
-------------------------- lexer.l
; some definitions ...
digit [0-9]
%%
;; Skip whitespaces
[ \t\n]+ (yycontinue)
;; Tokens that are also Scheme symbols
("<=" | "<" | ">=" | ">" | "<>" | "-" | "+" | "*" | "/" | "=")
(string->symbol (yytext))
{digit}+ (cons 'INTEGER (string->number (yytext)))
;; special punctuation
"[" 'LBRACKET
"]" 'RBRACKET
"(" 'LPAREN
")" 'RPAREN
"." 'DOT
";" 'SEMICOLON
;; ... etc.
;; end of input
<<EOF>> '*eoi*
--------------------------
> If you could show me how to add parenthesis to your simple grammar
> example it would be most helpful!
There is a more involved example that comes with the standard distribution
of LALR:
http://www.iro.umontreal.ca/~boucherd/Lalr/documentation/lalr.html
Cheers!
Dominique