How Do You Construct a Parse Tree for the Expression (a, (a, a))?

  • Thread starter Thread starter toothpaste666
  • Start date Start date
  • Tags Tags
    Tree
Click For Summary
To construct a parse tree for the expression (a, (a, a)) using the grammar provided, the starting symbol should be S. The commas in the expression represent elements in the list L, which can be constructed using the rules of the grammar. The parentheses are not special symbols; however, their presence affects the structure of the parse tree, as removing them could lead to ambiguity in the grammar. The parse tree should clearly show the connections between substitutions, typically represented with arrows. It is essential to refer to course materials for the correct method of drawing the tree.
toothpaste666
Messages
516
Reaction score
20

Homework Statement



Consider the grammar
S←(L)
S←a
L←L,S
L←S

draw a parse tree for the expression (a, (a, a))

The Attempt at a Solution


I can't tell if the starting symbol should be S or L. Also what do the commas mean? The examples we did in class had plus and minus signs and stuff like that but we didn't have an example of what to do when there is a comma. Also is there a difference if the parenthesis weren't there around the L in the first one?
 
Physics news on Phys.org
attachment.php?attachmentid=69448&d=1399323730.jpg
This is my attempt at drawing it so far. I don't know how the arrows connecting them should be drawn though.
 

Attachments

  • parsetree.jpg
    parsetree.jpg
    7.1 KB · Views: 487
There is nothing special about the parentheses and commas. They are just symbols. The question would be exactly the same if the grammar was
S←xLy
S←a
L←LzS
L←S
and the string was xazxazayy.

Also is there a difference if the parenthesis weren't there around the L in the first one?
Since there is nothing special about the parentheses, the answer is clearly "yes". Without the parentheses, the grammar would have two rules
S←L
L←S
so the parse tree would be ambiguous, and could be infinitely big.

Your attempt looks OK. Check your course notes or textbook for how you are expected to draw the tree including arrows.
 
  • Like
Likes 1 person
thank you! I think I will draw an arrow connecting each substitution.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 26 ·
Replies
26
Views
2K
Replies
5
Views
5K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K