Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logic Language A Context Free Grammar To define a Language For which every string is a logic expression.

Similar presentations


Presentation on theme: "Logic Language A Context Free Grammar To define a Language For which every string is a logic expression."— Presentation transcript:

1 Logic Language A Context Free Grammar To define a Language For which every string is a logic expression

2 Example Strings in Logic Language Strings in Logic Language Example 1 x = y   x  z. Example 2 x = u  (w  v) ; y = u  x. Source strings are processed by a lexical analyser which replaces variable names such as a,b,.. x,y,z with a token where i is index of lower case letter in “a,b,.. x,y,z ”. Lexical analysis replaces logical constants such as “T” and “F” with where i is the index of constant in “T,F”. Operators are replaced as follows :  by ~,  by *,  by + and remaining operator symbols =,(,) are left as is.

3 Logic Language Grammar Grammar Rules N  SM. M  ;S M  S  v=E E  TR R  +E R  T  FU U  *T U  F  ~F F  (E) F  v F  c Derivation of v=v;v=v+V. N  SM.  v=EM.  v=TRM.  v=FURM.  v=vURM.  v=vM.  v=v;S.  v=v;S.  v=v;v=OR.  v=v;v=TUR.  v=v;v=FUR.  v=v;v=vUR....

4

5 First Sets N:v M:;# S:v E:vc~( R:+# U:*# T:vc~( F:vc~( Follow Sets N:$ M:. S:;$. E:;$.) R:;$.) U:+;$.) T:+;$.) F:*+;$.) Rule Signatures 1:v 2:; 3:. 4:v 5:vc~( 6:+ 7:;$.) 8:vc~( 9:* 10:+;$.) 11:v 12:c 13:~ 14:( Nonterminal- Terminal-Rule Triples Nv1 M;2 M.3 Sv4 Ev5 Ec5 E~5 E(5 R+6 R;7 R$7 R.7 R)7 Tv8 Tc8 T~8 T(8 U*9 U+10 U;10 U$10 U.10 U)10 Fv11 Fc12 F~13 F(14

6 N | v=v+v*v.$ | Replace N by R1 : SM. | v=v+v*v.$ | Replace S by R4 : v=EM. | v=v+v*v.$ | Match : =EM. | =v+v*v.$ | v Match : EM. | v+v*v.$ | v= Replace E by R5 : TRM. | v+v*v.$ | v= Replace E by R8 : FURM. | v+v*v.$ | v= Replace R by R11 : vURM. | v+v*v.$ | v= Match : URM. | +v*v.$ | v=v Replace R by R10 : RM. | +v*v.$ | v=v Replace E by R6 : +EM. | +v*v.$ | v=v Match : EM. | v*v.$ | v=v+ Replace E by R5 : TRM. | v*v.$ | v=v+ Replace E by R8 : FURM. | v*v.$ | v=v+ Replace R by R11 : vURM. | v*v.$ | v=v+ Match : URM. | *v.$ | v=v+v Replace R by R9 : *TRM. | *v.$ | v=v+v Match : TRM. | v.$ | v=v+v* Replace E by R8 : FURM. | v.$ | v=v+v* Replace R by R11 : vURM. | v.$ | v=v+v* Match : URM. |.$ | v=v+v*v NSM SvE= ETR TFU Fv U# RE+ ETR TFU Fv UT* TFU Fv U# R# M# M - R - U - F - v T - v U - v* F - v T - vv* E - vv* R - vv*+ U - F - v T - v E - vvv*+ S - vvvv*+= N - vvvv*+=

7 Grammar N -> SM. M -> ;N M -> # S -> OR R -> +S R -> # O -> TU U -> *T U -> # T -> F T -> ~F F -> (S) F -> v F -> c First Sets N:~(vc M:;# S:~(vc R:+# O:~(vc U:*# T:~(vc F:(vc Follow Sets N:$. M:. S:;$). R:;$). O:+;$). U:+;$). T:*+;$). F:*+;$). Rule Signatures 1:~(vc 2:; 3:. 4:~(vc 5:+ 6:;$). 7:~(vc 8:* 9:+;$). 10:(vc 11:~ 12:( 13:v 14:c Triples N~1 N(1 Nv1 Nc1 M;2 M.3 S~4 S(4 Sv4 Sc4 R+5 R;6 R$6 R)6 R.6 Triples-Cont O~7 O(7 Ov7 Oc7 U*8 U+9 U;9 U$9 U)9 U.9 T(10 Tv10 Tc10 T~11 F(12 Fv13 Fc14

8 Postfix : Infix Expression - w = x * (y + ~z), Postfix Expression - w x y z ~ + * = xyzw 100? Operand Stack Next Postfix Character w wx w xy w x yz w x y z~

9 Postfix : Infix Expression - w = x * (y + ~z), Postfix Expression - w x y z ~ + * = xyzw 100? Operand Stack Next Postfix Character w x y 1+ w x 1* w 1 1 1

10 Attributed Grammars An attribute is a record associated with a grammar character; it can be used for generating target code. In example, A is postfix string. Rule #RuleSemantic ActionExplanation 1 E A  T A R A A 0 = A 1 & A 2 Concatenate postfix 2 R A  + T A R A A 0 = A 2 & A 3 & “+” Insert + after 2 nd operand 3 R A  A 0 = Postfix empty 4 T A  a U A A 0 = “a” & A 2 Operands in infix order 5 U A  * a U A A 0 = “a” & “*” & A 3 Insert * after 2 nd operand 6 U A  A 0 = Postfix empty

11 Sample Parse

12 Lexical Analysis In order to partition the processing involved in translating an expression in logic language (representing a circuit) into an internal representation suitable for simulating the circuit, the processing is divided into several stages: Lexical Analysis source code is converted into tokens consisting of a syntactic code used in parsing and a semantic code used to generate target language an “a” is converted to a token where the syntactic code “d” signifies data and the semantic code “1” indicates where the value can be accessed. Possible variables are denoted by “abcd..z” with semantic code being offset from “a”. Constants “T” and “F” are converted to and a “+” is converted to a token, “*” to, “~” to and “=“ to

13 Lexical Analysis - continued Syntax directed parsing & translation into postfix Tokens are parsed where the syntactic codes are terminals in the grammar Postfix excecution to test the behaviour of the circuit. Postfix evaluation

14 Parse a+a*a$ E | a+a*a$ | X by R1 : TR | a+a*a$ | X by R4 : aUR | a+a*a$ | Match : UR | +a*a$ | a X by R6 : R | +a*a$ | a X by R2 : +TR | +a*a$ | a Match : TR | a*a$ | a+ X by R4 : aUR | a*a$ | a+ Match : UR | *a$ | a+a X by R5 : *aUR | *a$ | a+a Match : aUR | a$ | a+a* Match : UR | $ | a+a*a X by R6 : R | $ | a+a*a X by R3 : | $ | a+a*a Match : | | a+a*a$ Attribute Evaluation X by R3 : | $ | a+a*a R = X by R6 : R | $ | a+a*a U = X by R5 : *aUR | *a$ | a+a U = a 3 * X by R4 : aUR | a*a$ | a+ T = a 2 a 3 * X by R2 : +TR | +a*a$ | a R = a 2 a 3 * + X by R6 : R | +a*a$ | a U = X by R4 : aUR | a+a*a$ | T = a 1 a 2 a 3 * + X by R1 : TR | a+a*a$ | E = a 1 a 2 a 3 * + Rule #RuleSemantic Action 1 E A  T A R A A 0 = A 1 & A 2 2 R A  + T A R A A 0 = A 2 & A 3 & “+” 3 R A  A 0 = 4 T A  a U A A 0 = “a” & A 2 5 U A  * a U A A 0 = “a” & “*” & A 3 6 U A  A 0 =

15 Rule #RuleSemantic Action 1 E A  T A R A A 0 = A 1 & A 2 2 R A  + T A R A A 0 = A 2 & “+” & A 3 3 R A  A 0 = 4 T A  a U A A 0 = “a” & A 2 5 U A  * a U A A 0 = “a” & “*” & A 3 6 U A  A 0 = E T R a U + T R * a U a U a a * a * a a + a a * a + Syntax Directed Translation of Infix to Postfix

16 Rule #RuleSemantic Action 1 E A  T A R A A 0 = A 1 & A 2 2 R A  + T A R A A 0 = A 2 & “+” & A 3 3 R A  A 0 = 4 T A  a U A A 0 = “a” & A 2 5 U A  * a U A A 0 = “a” & “*” & A 3 6 U A  A 0 = E T R a U + T R * a U a U a a * a * a a + a a * a + E 1 = T 2 R 3 T 2 = aU 4 U 4 = *aU 5 U 5 = R 3 = +T 6 R 7 T 6 = aU 8 U 8 = R 7 =

17 LL(1) Syntax Directed Translation Infix to Postfix Parsing: – Start with the nonterminal representing the language as the unmatched sentential form (USF) and also the postfix output (PO) – Repeat remaining source string is empty or until failure Let X be the leftmost character in USF, t leftmost character of remaining source If X is terminal it must the first character of the remaining source (otherwise failure) If X is nonterminal then –Select the rule R k where k is Parse table entry in row X, column t and apply this rule to X –Let X be the leftmost nonterminal in PO and apply the semantic action S k to it.

18

19 Rule #RuleSemantic Action 1 E A  T A R A A 0 = A 1 & A 2 2 R A  + T A R A A 0 = A 2 & “+” & A 3 3 R A  A 0 = 4 T A  a U A A 0 = “a” & A 2 5 U A  * a U A A 0 = “a” & “*” & A 3 6 U A  A 0 = E1 T2 R3 a U4 + T6 R7 * a U5 a U8 a a * a *a a + a a * a + E 1 = T 2 R 3 T 2 = aU 4 U 4 = a*U 5 U 5 = R 3 = +T 6 R 7 T 6 = aU 8 U 8 = R 7 = P = E1 = T2R3 = aU4R3 = a a*U5 R3 = a a*R3 = a a * T6 + R7 = a a * aU8 + R7 = a a * a + R7 = a a * a +

20 GrammarParse v=v*vOutputPostfix 1. N  SM. 2. M  ;S 3. M  # 4. S  v=E 5. E  TR 6. R  +E 7. R  # 8. T  FU 9. U  *T 10. U  # 11. F  v 12. F  c 13. F  ~F 14. F  (E) N | v=v*v.$ | Replace N by R1 : SM. | v=v*v.$ | Replace S by R4 : v=EM. | v=v*v.$ | Match : =EM. | =v*v.$ | v Match : EM. | v*v.$ | v= Replace E by R5 : TRM. | v*v.$ | v= Replace E by R8 : FURM. | v*v.$ | v= Replace R by R11 : vURM. | v*v.$ | v= Match : URM. | *v.$ | v=v Replace R by R9 : *TRM. | *v.$ | v=v Match : TRM. | v.$ | v=v* Replace E by R8 : FURM. | v.$ | v=v* Replace R by R11 : vURM. | v.$ | v=v* Match : URM. |.$ | v=v*v Replace R by R10 : RM. |.$ | v=v*v Replace E by R7 : M. |.$ | v=v*v Replace M by R3 :. |.$ | v=v*v Match : | $ | v=v*v. NSM SvE= ETR TFU Fv UT* TFU Fv U# R# M# M - R - U - F - v T - v U - v* F - v T - vv* E - vv* S - vvv*= N - vvv*=

21 GrammarParse v=v*vOutput Postfix 1. N  SM. 2. M  ;S 3. M  # 4. S  v=E 5. E  TR 6. R  +E 7. R  # 8. T  FU 9. U  *T 10. U  # 11. F  v 12. F  c 13. F  ~F 14. F  (E) N | v=v+v*v.$ | Replace N by R1 : SM. | v=v+v*v.$ | Replace S by R4 : v=EM. | v=v+v*v.$ | Match : =EM. | =v+v*v.$ | v Match : EM. | v+v*v.$ | v= Replace E by R5 : TRM. | v+v*v.$ | v= Replace E by R8 : FURM. | v+v*v.$ | v= Replace R by R11 : vURM. | v+v*v.$ | v= Match : URM. | +v*v.$ | v=v Replace R by R10 : RM. | +v*v.$ | v=v Replace E by R6 : +EM. | +v*v.$ | v=v Match : EM. | v*v.$ | v=v+ Replace E by R5 : TRM. | v*v.$ | v=v+ Replace E by R8 : FURM. | v*v.$ | v=v+ Replace R by R11 : vURM. | v*v.$ | v=v+ Match : URM. | *v.$ | v=v+v Replace R by R9 : *TRM. | *v.$ | v=v+v Match : TRM. | v.$ | v=v+v* Replace E by R8 : FURM. | v.$ | v=v+v* Replace R by R11 : vURM. | v.$ | v=v+v* Match : URM. |.$ | v=v+v*v Replace R by R10 : RM. |.$ | v=v+v*v Replace E by R7 : M. |.$ | v=v+v*v Replace M by R3 :. |.$ | v=v+v*v Match : | $ | v=v+v*v. R 1 => "NSM" R 2 => "M;S" R 3 => "M#" R 4 => "SvE=" R 5 => "ETR" R 6 => "RE+" R 7 => "R#" R 8 => "TFU" R 9 => "UT*" R 10 => "U#" R 11 => "Fv" R 12 => "Fc" R 13 => "FF~" R 14 => "FE“ Output is LHS Followed by Postfix of RHS NSM SvE= ETR TFU Fv U# RE+ ETR TFU Fv UT* TFU Fv U# R# M# M - R - U - F - v T - v U - v* F - v T - vv* E - vv* R - vv*+ U - F - v T - v E - vvv*+ S - vvvv*+= N - vvvv*+=


Download ppt "Logic Language A Context Free Grammar To define a Language For which every string is a logic expression."

Similar presentations


Ads by Google