Referencing Multiple Non-Terminals in Production with Same Name in Yacc

38 views Asked by At

Let's assume I have the production:

Expression                             // These are my semantic actions
  : Expression PLUS_TOKEN Expression ( create_node(Expression, Expression) )
  | SimpleExpression                 ( SimpleExpression ) (* Returns a node of type expression *)

Notice how I can't tell which Expression is which in my top most production's semantic action. How do I refer to the left and right Expression? What if I have three or more 'Expression's appear in the same production?

1

There are 1 answers

0
EsotericLanguage On

Reference: http://www.smlnj.org/doc/ML-Yacc/mlyacc002.html

According to ML-Yacc documentation we refer to non-terminals with the following notation:

{non-terminal}{n+1}

such that n is the number of occurrences of the non-terminal to the left of the symbol. If n equals one then we can just use the non-terminal name.

Hence, the above example would look like this:

Expression                             // These are my semantic actions
  : Expression PLUS_TOKEN Expression ( create_node(Expression1, Expression2) )
  | SimpleExpression                 ( SimpleExpression ) (* Returns a node of type expression *)