• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to determine the value type in compiler when evaluate the syntax tree?

 
fei peng
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,every one.
I'm writing a interpreter of a expression language by javacc,javacc works well ,it parses it ,and genrate a right syntax tree.but when I evaluate this tree,a problem appeared:
I was forced not to distinguish the different form of the value types of the identifiers in expression,and there is no type define statements,just one expression, but there is two types of value,the numric and the string(string type is only used as function argumnet),for example: in expression "a*b","a" should be numeric,but in "sum(a)" ,"a" should be string,in tree node ,they all are identifiers,how can I resolve this?
The EBNF of the language is following(just focus on the production 9-12,identifiers act as function name at the same time):
1.)formalu->logicExpression
2.)logicExpression->AndExpression {|| AndExpression}
3.)AndExpression->EqualityExpression {&& EqualityExpression}
4.)EqualityExpression->RelationalExpression {(==|!=) RelationalExpression}
5.)RelationalExpression->AdditiveExpression {(>|<|>=|<=) AdditiveExpression}
6.)AdditiveExpression->MultiplicativeExpression {(+|-) MultiplicativeExpression}
7.)MultiplicativeExpression->UnaryExpression {(*|/|%) UnaryExpression}
8.)UnaryExpression->[(+|-)]literal
9.)literal-><INTEGER_LITERAL>|<FLOATING_POINT_LITERAL>|<COLUMNNAME>|<IDENTIFIER> [arguments]|(formalu)
10.)arguments->([argumentList])
11.)argumentList->argument {,argument}
12.)argument->{argumentList}|formula
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know that you can do this syntactically. By eliminating the need to declare types for variables, you have eliminated the ability to enforce how a variable can be used.

is going to be valid no matter if a = 3 or a = "Hello, World"

The most that you can do here is have some sort of run-time exception that halts processing and states somethign like, "You cannot multiple "Hello, World" by anything." or something like that.
 
fei peng
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,Joel,
your answer support my guess: only by itself the compiler can't distinguish this two semantic,now there is only way outside the compiler needed to be found
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic