LangProc / langproc-2017-cw

1 stars 4 forks source link

" * " Special symbol vs Operator #3

Open victoralxdr opened 6 years ago

victoralxdr commented 6 years ago

" * " (asterisk) can be both a special symbol (pointer) and an operator (multiply) as a basic C++ token: how should we deal with this in our lexer ? should we look at context while lexing the token or deal with it during parsing ?

example: int p; (pointer) int i = tp; (multiply)

m8pple commented 6 years ago

As you say, you can't easily tell how the symbol is being used during lexing. To your examples I'd add:

int *p;
int y = 5 * * p;

and

int *p;
int y = 5 * * * (& p);

You could try to track context in the lexer, but this becomes quite complex, as you end up having to build a parser in the lexer in order to give symbols to the parser. So this decision is something you should leave to the parser, with a single token that represents a * for both contexts.

In the parser you'd then handle it as something like:

Expr : Mult
         | Mult TAdd Expr

Mult : Deref
          | Mult TStar Deref

Deref : Primitive
          | TStar Primitive

Primitive : Id
         | Num
         | TLeft Expr TRight

(Note that I'm just making that fragment up).