From: Mark Probert Date: 2002-09-24T23:11:33+09:00 Subject: Re: Lexing ... At 05:39 AM 9/24/2002 +0900, Phil wrote: > > > >Given the recent discussion on lexer/parsers for Ruby, I thought that I > >would do something I have been meaning to do for a while, port Coco/R to > >ruby. > > > >Cool. I also don't like having a seperate lexer and parse (like using lex >and yacc) so this sounds interesting. > >Can we take a look at what you've got so far? There is not too much to show at the moment. I scan a ruby-ised version of the attributed grammar into token. That is the -easy- part :-). I think that in a few weeks I will have something "real" to share. There is a lot of fine-print in working out the parser... FWIW, a simple Coco/R grammar looks like (this is the expr example from the Dragon book): --------< expr.atg >---------------- $C # generate the driver COMPILER Expr # This is a simple expression calculator #----------------- Scanner Specifications ---------------------- CHARACTERS letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz". digit = "0123456789". tab = CHR(9). eol = CHR(10). TOKENS ident = letter {letter|digit}. number = digit { digit }. IGNORE eol+tab COMMENTS FROM "--" TO eol #--------------------Parser Specification ------------------- PRODUCTIONS Expr = StatSeq . StatSeq = SYNC { Stat ";" SYNC} . Stat = Expression (. puts "#{n}" .) . Expression = Term { "+" Term (. a += b .) | "-" Term (. a -= b .) } . Term = Factor { '*' Factor (. a *= b .) | '/' Factor (. a /= b .) } . Factor = (. VAR sign = 1 .) [ "-" (. sign = -1 .) ] ( Number | "(" Expression ")" ) (. n *= sign .) . Number = number (. n = scanner.get_string.to_i .) . Ident = ident (. n = scanner.get_string .) . END Expr.