Leftmost m the rule to be followed is

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: <M> can be <P>*<P>. So we go left and check the first <P>, <P> can an Integer and finally a 5. Now, we have to go back to the first level we left incomplete, which is the <P>*<P>, because we need to complete the second <P>, so that <P> is an Integer and finally a 2. Now we need to go back until an incomplete level, that would be <E> to the top, and we can say now that <E> can be <M> + <M> One more example, consider the following expression: 5 * (2 + 10) Homework. Do the derivation trees for the following two expressions. • (5 * 2) + 10 • 5 * 2 * 10 2. Syntax Diagrams As for lexical analysis there is a way to express rules using text (Regular expressions) or using a visual diagram (DFA); for syntax we also have these two options. We can use the BNF or EBNF (text- based) or we can use syntax diagrams (visual- based). The previous grammar can be expressed with the followings diagrams: NOTE: The grammar here was updated (and it is different from the one reviewed in class) to make it simpler. It is the same that appears in the slides. <E> à༎ <M> {+ <M>} <M> à༎ <P> {* <P>} <P> à༎ (<E>) | Integer Notice that terminal elements are symbolized with circles, when non- terminals are represented with rectangles. Notice that the arrowheads represent the flow on the diagram, “or” expressions are represented with diverse paths coming out from the root of each diagram. Now, what if we want to define a new grammar that is able to recognize logical, relational, and arithmetical operations? And use it to evaluate the following expression: 10 + 20 > 15 & - 10 != 1 | 20 / 10 + 1 > 5 The first step is to define the precedence of all this operations. So logical operations are the ones with higher precedence, then comes the relational, and then arithmetical. Notice that this precedence is defined by how one rule calls (uses) a subsequent rule. The diagrams below represent the grammar, where <E> is the starting symbol. Notice that according with the diagram, <E> is expressed in terms of <A>. Notice that the precedence of the operations is defined by how one rule calls (uses) a subsequent rule. Homework. Generate the BNF or EBNF for these diagrams....
View Full Document

This note was uploaded on 09/29/2013 for the course CSE 340 taught by Professor Richa during the Spring '13 term at Arizona State University.

Ask a homework question - tutors are online