[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Parser for simple calculator



There are examples similar to this in my Compiler Design textbook, which uses SableCC.  

See  cs.rowan.edu/~bergmann/books

(Chapters 2 and 4 contain the SableCC examples)



Seth D. Bergmann             Associate Professor
Computer Science             bergmann@xxxxxxxxx
Rowan University               856-256-4500  ext. 3197
Glassboro NJ 08028          Fax: 856-256-4741
-----Original Message-----
From: sablecc-discussion-bounces+bergmann=rowan.edu@xxxxxxxxxxxxxxxxx [mailto:sablecc-discussion-bounces+bergmann=rowan.edu@xxxxxxxxxxxxxxxxx] On Behalf Of Prof. Dr. Boris Hollas
Sent: Wednesday, August 18, 2010 5:34 AM
To: sablecc-discussion@xxxxxxxxxxxxxxxxx
Subject: Parser for simple calculator

Hello,

I want to implement the standard calculator example with SableCC. To make
things even simpler, I only consider addition.

I want the program to read integer-valued expression from the console and
evaluate them, eg:
~$ calc 1+2+3
6

So far, I've created this grammar, which compiles with SableCC:


Package calc;

Tokens
	number = ['0' .. '9']+;
	plus = '+';

Productions
	expr = {number} [n:]number |
		{plus} [e:]expr plus [n:]number;


Unfortunately, I was not able to determine how to create a complete
calculator with action code from the SableCC manual.

My questions:
- How does action code have to be added?
- Do I have to supply a main function and if, how is it integrated?
- Which files must be compiled with SableCC or javac and in which order?
- Is it necessary to supply a runtime library to execute the resulting
calculator?
- Assume I want to create a lexer only: on input 1+2+3, the lexer should
give the token string "number plus number plus number" as output. How is
this accomplished?

Best regards,
Boris



_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@xxxxxxxxxxxxxxxxx
http://lists.sablecc.org/listinfo/sablecc-discussion