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

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