-2

I need assistance regarding generating the parse tree and the syntax tree. My version of the solutions are the images below but I don't know if they are correct or way off.

Given the Grammar: S-->aSb | e

And the String: enter image description here

Generate the: (i) Parse Tree, and (ii) Syntax Tree

My Solutions: i)enter image description here

II)enter image description here

  • The string "a3b3" can't even parse. "3" is not a legal terminal, only "a" and "b" are. Check the question. – kaby76 Apr 03 '22 at 20:45
  • @kaby76 Yeah but the thing is basically that the 3 is an exponent in this case it's not a3b3 its more so a^3b^3 but i will edit the question to be more clear – zed unseened Apr 03 '22 at 20:55
  • 1
    Ok, now I understand. a3 means `aaa`--a sequence of three `a`s. It's been that way for many decades. Besides, the exponents are not actually defined in the productions: there's no "[0-9]+" is there? You need to redo the parse tree first. – kaby76 Apr 03 '22 at 21:10
  • You don't need an image to type a³b³. If your keyboard doesn't have them, you can use `³` or `3`. (But not in comments. In comments you need to know how to use your keyboard :-) ) – rici Apr 03 '22 at 21:18
  • Your parse tree for input `aaabbb` should be `(s a (s a (s a s b) b) b)` as an s-expression. (If you want, you can use Antlr4 on `grammar AB; s : 'a' s 'b' | ;` to verify the result.) – kaby76 Apr 03 '22 at 21:24

1 Answers1

2

A parse tree (or a syntax tree) needs to correspond to the grammar. The easiest way to construct one is directly from the grammar, since this is exactly what your parser will do. Get out a nice big sheet of paper, and start writing out the derivation.

In this case, you can work forwards because it should be obvious how the derivation proceeds. At every step in the following derivation, try to figure out how I knew which alternative to select. (It's not rocket science.)

Remember that a grammar is simply a game with symbols. A production like S → a S b means (exactly) "replace some S with a S b. S → p q r | x y z is just a short way of writing two productions: S → p q r and S → x y z. The ε symbol means "nothing"; it's just there so that you can see it, since "nothing" is invisible. So S → ε means that you can replace an S with nothing (that is, just delete it).

At each step, you choose one of the rules which applies. Whichever one you think is appropriate. You can stop when there are no rules which apply. That's your goal: to stop exactly at the point when the derivation has produced the string you are parsing.

So it goes like this:

String so far rule result
S S → a S b a S b
a S b S → a S b a a S b b
a a S b b S → a S b a a a S b b b
a a a S b b b S → ε a a a b b b

Now, to make that into a tree, start with the root symbol at the root of the tree, and then write the replacement of a symbol as its children: (non-substitute symbols -- the terminals -- don't have children. They're leaves.)

            S
            |
____________|____________
|           |           |
a           S           b
      ______|______
      |     |     |
      a     S     b      
            |
            …
rici
  • 234,347
  • 28
  • 237
  • 341