# [antlr-interest] Help With Simple Example

Vitucho power_giordo at yahoo.com.ar
Tue Jul 27 16:33:27 PDT 2010

```Hi there Friends!.
I am new to this, i and have a lot of doubts, but don't worry, i will try to
handle.. histroy probes us that manking learn after more or less try and
errors :).
But... there is a thing that i just don't understand.. i am trying to create
a parser for a linear expression:
Remeber that linear expression only contains linear terms, and a linear term
is just a coefcient multiply by a variable.
So: 2 x + 3 y + 5 z is a linear expression and 2 x is a linear term. So i
want to keep it simple for now. The think is that 2 x1 + 2 x2 + 3 x3 may be
a linear expression in my program too. So the name of the variable, must
always start with a letter but it can be followed by a number, that number
is like the index of the x var.

So with the help of copy paste i wrote this and looking to another examples,
i wrote this:

grammar Simplex;

tokens
{
PLUS 	= '+' ;
MINUS	= '-' ;
MULT	= '*' ;
DIV	= '/' ;
}

expr	: term ( ( PLUS | MINUS )  term )*
{
System.out.println ("GREAT");
};

term	: (NUMBER)(VAR);/*|(NUMBER)(WHITESPACE)(VAR);*/

NUMBER	: DIGIT+;

WORD	: LETTER+;

VAR 	: LETTER(DIGIT)*;

WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ 	{ \$channel = HIDDEN; }
;

fragment DIGIT	: '0'..'9' ;

fragment LETTER	: 'a'..'z'|'A'..'Z'|'_';

The _thing_ is in the lexer rule named VAR, cuz a wanted that a var must
start with a letter, then followed by letters and only in at end it can be
zero o more number.
I tried with the example 2x1+ 2 x2 +	3	x3 The last space is actually a tab.
And all goes OK :).

If i want a var to start always with a letter and then it can be followed by
one or more letter or numbers alike.
VAR	: LETTER((LETTER|DIGIT)*);

But wait, here comes the end of this post:
I have doutbs from before that i was unable to auto explain to me, how the
the parsers skips the spaces and the tabs?.
I Should have to put the grammar rule term, like
term : (NUMBER)(WHITESPACE)(VAR); instaed, but it wasn't necesary,
so?...mmmmmmm.. whats the matter..?.

ANd another doubt is the think of

tokens
{
PLUS 	= '+' ;
MINUS	= '-' ;
MULT	= '*' ;
DIV	= '/' ;
}

it isn't the same putting token there that putting they like lexer rules.
PLUS 	: '+' ;
MINUS : '-' ;
MULT	: '*' ;
DIV	: '/' ;

This is all, i guess is something simple to answer, and maybe is it out
there in someone blog or tutorial, but i doesn't find it, so sorry if i re

The grammar is called simplex.g beacuse i wrote a program in java to solve
simplex ecuations, btw if you want te see it just visit
http://sourceforge.net/projects/complex/
http://sourceforge.net/projects/complex/ .

I guess i will asking something more, cuz later i will plant to parse linear
expression it non canonical way: like:
example 1) 2 x - 3 y equally to 2 x + (-3) y actually (canonical)
example 2) (2 x - 3 y) * 3 equally to 6 x + (-9) y (canonica)

But i just start with the canonical way, like you see. Trying to replace my
home made parser with one more powerfull that let me do this transformation
(linear expression non canonical to canonical).

Okey, yes, this is it the end jejej.
Cya!!.

--
View this message in context: http://antlr.1301665.n2.nabble.com/Help-With-Simple-Example-tp5344600p5344600.html
Sent from the ANTLR mailing list archive at Nabble.com.
```