S - Intranet - Department of Computer Science: Login
S - Intranet - Department of Computer Science: Login
S - Intranet - Department of Computer Science: Login
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Evaluate the expression<br />
• The previous specification only indicates the success or<br />
failure <strong>of</strong> a parser. No semantic action is associated with<br />
grammar rules.<br />
• To calculate the expression, we must add java code in<br />
the grammar to carry out actions at various points.<br />
• Form <strong>of</strong> the semantic action:<br />
expr:e1 PLUS expr:e2<br />
{: RESULT = new Integer(e1.intValue()+ e2.intValue()); :}<br />
– Actions (java code) are enclosed within a pair {: :}<br />
– Labels e2, e2: the objects that represent the corresponding terminal or nonterminal;<br />
– RESULT: The type <strong>of</strong> RESULT should be the same as the type <strong>of</strong> the<br />
corresponding non-terminals. e.g., expr is <strong>of</strong> type Integer, so RESULT is <strong>of</strong><br />
type integer.<br />
64