Questions - BNF: 2. Consider the following EBNF syntax.

Which of the sentences following are not legal according to the syntax for sneeches? Why? ::= ! " # # <sneech> $ ! $ " % <&ander>' <sneech>

<sneech>

<&ander> ::= ( )*) " + , " # - <&ander> $ .. #!$ 2. #)*)!$ /. ! 0. !!!!! 1. ---!! 2. +++++!! 3. #)*)+ 4. )*)+! 5. !)*)+ .6. -+!)*)!! 2. Consider the BNF <&ex7r> ::= <&ex7r> or <&ter8> " <&ter8> <&ter8> ::= <&ter8> and <&factor> " <&factor> <&factor> ::= not <&factor> " #<&ex7r>$ " true " false Constr9ct a 7arse tree for the sentence not #true or false$. :how that this BNF generates all &oolean ex7ressions. Solutions - BNF .. • • • • • • • • • .. #!$ Not legal it can &e re7resented as #!$! &eca9se of the r9le of sneech # # <sneech $ ! $. sneech ;> ! .it can &e re7resented as ! &y the r9le of sneech ;> ! . 2. #)*)!$ Not legal <he asteri= sho9ld &e o9tside of the right 7arenthesis> if we re7resent sneech ;> # # <sneech $ ! $ li=e this. /. ! ?egal sneech 0. <he asteri= can &e 7resented one at a ti8e and not rec9rsi@e#lin=$ to extend the asteri=s . 1. ---!! Not legal> Asing the r9le &ander ;> # - <&ander>$> we cannot generate ! after 2. +++++!! Not legal sho9ldn t ha@e !! 3. #)*)+ Not legal> no closing 7arenthesis. 4. )*)+! ?egal 5. !)*)+ Not legal> sho9ld &e <&ander>< sneech>not sneech &ander. .6. -+!)*)!! Not legal> sho9ldn t ha@e !!

2. Constr9cting a 7arse tree for not (true or false) &ex7r => &ter8 &ex7r => &factor &ex7r => not &factor &ex7r => not #&ex7r$ &ex7r=> not # &ex7r or &ter8 $ &ex7r => not # &ter8 or &factor $ &ex7r => not # &factor or false $ &ex7r => not # true or false $ <o show the gra88er generates all &oolean ex7ressions &ex7r => &ter8 &ex7r => &factor &ex7r => true

&ex7r => &ter8 &ex7r => &factor &ex7r => false &ex7r => &ter8 &ex7r => &factor &ex7r => not &factor &ex7r => not true &ex7r => &ter8 &ex7r => &factor &ex7r => not &factor &ex7r => not false &ex7r => &ter8 &ex7r => &ter8 and &factor &ex7r => &ter8 and false &ex7r => &factor and false &ex7r => true and false &ex7r => &ex7r or &ter8 &ex7r => &ter8 or &ter8 &ex7r => &factor or &ter8 &ex7r => tr9e or &ter8 &ex7r => tr9e or &factor &ex7r => true or false &ex7r => &ter8 &ex7r => &factor &ex7r => not &factor &ex7r => not &ex7r &ex7r => not &ter8 &ex7r => not &ter8 and &factor &ex7r => not &factor and &factor &ex7r => not tr9e and &factor &ex7r => not true and false &ex7r => &ter8 &ex7r => &factor &ex7r => not &factor &ex7r => not &ex7r &ex7r => not &ex7r or &ter8 &ex7r => not &ter8 or &ter8 &ex7r => not &factor or &ter8 &ex7r => not tr9e or &ter8 &ex7r => not tr9e or &factor &ex7r => not true or false .

Using Static Scoping Rules:.$ for the declaration of Daria&le E. $.na8e> call.@al9eGres9lt> and call. is declared$. which has 8ade this 7artic9lar call to :9&.. Fn this case> the @al9e of E is declared as . Here the program will print the value of X as 5>&eca9se the following ste7s will occ9r. for the declaration of E. When no declaration for E is fo9nd in :9&.reference are different 7ara8eter.6 in s9&2 and hence thix @al9e is 9sed for E in s9&.<he co87iler searches the dyna8ic 7arent of :9&. • • <he co87iler first searches the c9rrent 7roced9re #s9&.. (s9&.6C s9&. . Fn the 8ain> the @al9e of E is declared as 1 and this @al9e is 9sed for E in :9&. x := .which is 8ain #<he static 7arent is nothing &9t the larger enclosing 9nit in which s9&. x := 1C s9&2C end..C begin (s9&. Here the program will print the value of X as 10>&eca9se the following ste7s will occ9r. begin (8ain.C y:=y). (8ain. and gets 7rinted. is the 7roced9re or s9&. What @al9e of x is 7rinted in 7roced9res sub1? Ander dyna8ic sco7ing r9les> what @al9e of x is 7rinted in 7roced9re sub1? program 8ainC var x : integerC procedure s9&. When no declaration for E is fo9nd in :9&.&y. • • <he co87er first searches the c9rrent 7roced9re #s9&.Scoping Rules: Bss98e the following 7rogra8 was co87iled and exec9ted 9sing static sco7ing r9les.$ for the declaration of Daria&le E.&y.. (s9&2.C end.<he co87iler searches the static 7arent of :9&. writeln# x = > x$ end. and this is 7rinted. for the declaration of E.y$C end FC :how &y one or 8ore exa87les of calls on 7roced9re F that call. • Using Dynamic Scoping Rules:. • Consider the following f9nction: f9nction F#x>y: integer$ ret9rn integer is &egin x:=x). procedure s9&2C var x : integerC begin (s9&2.7assing 8ethods.which is :9&2 # <he dyna8ic 7arent of :9&.C ret9rn#x.&y. <hat is> show calls that 7rod9ce different res9lts for the different &inding r9les.

Sign up to vote on this title
UsefulNot useful