From 33370917adde77f98b0955843b3222d9838a3fe8 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sun, 26 Feb 2006 20:50:51 +0000 Subject: [PATCH] removed most shift/reduce conflicts by moving EAss from Exp3 to Exp and thus disallowing many constructs if parenthesis aren't used --- Docsyntax.tex | 9 ++++++--- Parsyntax.y | 9 ++++++--- Printsyntax.hs | 2 +- syntax.cf | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Docsyntax.tex b/Docsyntax.tex index b22e544..a4040dc 100644 --- a/Docsyntax.tex +++ b/Docsyntax.tex @@ -80,11 +80,11 @@ All other symbols are terminals.\\ & {\delimit} &{\terminal{if}} {\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} {\nonterminal{Stm}} {\terminal{else}} {\nonterminal{Stm}} \\ & {\delimit} &{\terminal{while}} {\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} {\nonterminal{Stm}} \\ & {\delimit} &{\terminal{print}} {\nonterminal{Exp}} {\terminal{;}} \\ - & {\delimit} &{\nonterminal{Stm}} {\terminal{;}} \\ \end{tabular}\\ \begin{tabular}{lll} {\nonterminal{Exp}} & {\arrow} &{\nonterminal{Exp1}} {\nonterminal{BOp}} {\nonterminal{Exp1}} \\ + & {\delimit} &{\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} \\ & {\delimit} &{\nonterminal{Exp1}} \\ \end{tabular}\\ @@ -102,13 +102,16 @@ All other symbols are terminals.\\ {\nonterminal{Exp3}} & {\arrow} &{\nonterminal{Ident}} {\terminal{{$+$}{$+$}}} \\ & {\delimit} &{\nonterminal{Ident}} {\terminal{{$-$}{$-$}}} \\ & {\delimit} &{\nonterminal{Ident}} \\ - & {\delimit} &{\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} \\ & {\delimit} &{\nonterminal{Integer}} \\ & {\delimit} &{\terminal{{$-$}}} {\nonterminal{Exp3}} \\ & {\delimit} &{\nonterminal{Bool}} \\ & {\delimit} &{\terminal{readInt}} \\ & {\delimit} &{\terminal{readBool}} \\ - & {\delimit} &{\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} \\ + & {\delimit} &{\nonterminal{Exp4}} \\ +\end{tabular}\\ + +\begin{tabular}{lll} +{\nonterminal{Exp4}} & {\arrow} &{\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} \\ \end{tabular}\\ \begin{tabular}{lll} diff --git a/Parsyntax.y b/Parsyntax.y index 6fef6ba..8bf7c68 100644 --- a/Parsyntax.y +++ b/Parsyntax.y @@ -67,11 +67,11 @@ Stm : Type Ident '=' Exp ';' { SDecl $1 $2 $4 } | 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 } | 'while' '(' Exp ')' Stm { SWhile $3 $5 } | 'print' Exp ';' { SPrint $2 } - | Stm ';' { $1 } Exp :: { Exp } Exp : Exp1 BOp Exp1 { BExp $1 $2 $3 } + | Ident '=' Exp { EAss $1 $3 } | Exp1 { $1 } @@ -89,13 +89,16 @@ Exp3 :: { Exp } Exp3 : Ident '++' { postIncr_ $1 } | Ident '--' { postDecr_ $1 } | Ident { EVar $1 } - | Ident '=' Exp { EAss $1 $3 } | Integer { EInt $1 } | '-' Exp3 { ENeg $2 } | Bool { EBool $1 } | 'readInt' { EReadI } | 'readBool' { EReadB } - | '(' Exp ')' { $2 } + | Exp4 { $1 } + + +Exp4 :: { Exp } +Exp4 : '(' Exp ')' { $2 } ListStm :: { [Stm] } diff --git a/Printsyntax.hs b/Printsyntax.hs index aeb3258..1d10687 100644 --- a/Printsyntax.hs +++ b/Printsyntax.hs @@ -105,7 +105,7 @@ instance Print Exp where prt i e = case e of BExp exp0 bop exp -> prPrec i 0 (concatD [prt 1 exp0 , prt 0 bop , prt 1 exp]) EVar id -> prPrec i 3 (concatD [prt 0 id]) - EAss id exp -> prPrec i 3 (concatD [prt 0 id , doc (showString "=") , prt 0 exp]) + EAss id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "=") , prt 0 exp]) EInt n -> prPrec i 3 (concatD [prt 0 n]) ENeg exp -> prPrec i 3 (concatD [doc (showString "-") , prt 3 exp]) EBool bool -> prPrec i 3 (concatD [prt 0 bool]) diff --git a/syntax.cf b/syntax.cf index fae1234..dc7e659 100644 --- a/syntax.cf +++ b/syntax.cf @@ -16,6 +16,7 @@ SWhile. Stm ::= "while" "(" Exp ")" Stm ; -- SFor. Stm ::= "for" "(" Stm Exp ";" Exp ")" Stm ; SPrint. Stm ::= "print" Exp ";" ; +EAss. Exp ::= Ident "=" Exp; BExp. Exp ::= Exp1 BOp Exp1 ; op1. Exp1 ::= Exp1 Op1 Exp2 ; define op1 e1 o e2 = OpExp e1 o e2 ; @@ -26,7 +27,6 @@ define postIncr i = EPost i Plus ; postDecr. Exp3 ::= Ident "--" ; define postDecr i = EPost i Minus ; EVar. Exp3 ::= Ident ; -EAss. Exp3 ::= Ident "=" Exp; EInt. Exp3 ::= Integer ; ENeg. Exp3 ::= "-" Exp3 ; EBool. Exp3 ::= Bool ; @@ -36,7 +36,7 @@ EReadB. Exp3 ::= "readBool" ; coercions Exp 3 ; -_. Stm ::= Stm ";" ; +-- _. Stm ::= Stm ";" ; terminator Stm "" ; -- 2.39.2