Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

" * " Special symbol vs Operator #3

Open
victoralxdr opened this issue Jan 31, 2018 · 1 comment
Open

" * " Special symbol vs Operator #3

victoralxdr opened this issue Jan 31, 2018 · 1 comment

Comments

@victoralxdr
Copy link

" * " (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 = t*p; (multiply)

@m8pple
Copy link
Contributor

m8pple commented Jan 31, 2018

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants