Most likely fixed the precedence issue

This commit is contained in:
Clemens-Dautermann 2020-12-18 00:07:23 +01:00
parent 1a95fc2371
commit 6e1307c883
3 changed files with 113 additions and 30 deletions

View file

@ -19,14 +19,9 @@
<select /> <select />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="a54bb6de-191c-4bd1-91ab-3953adfc5dfb" name="Default Changelist" comment=""> <list default="true" id="a54bb6de-191c-4bd1-91ab-3953adfc5dfb" name="Default Changelist" comment="Lots of comments and cleanup">
<change afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/riderModule.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../test.s" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/contentModel.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/contentModel.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/modules.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/DevFunctions.cs" beforeDir="false" afterPath="$PROJECT_DIR$/DevFunctions.cs" afterDir="false" /> <change beforePath="$PROJECT_DIR$/DevFunctions.cs" beforeDir="false" afterPath="$PROJECT_DIR$/DevFunctions.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Generator/Generator.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Generator/Generator.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Parser/Parser.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Parser/Parser.cs" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Parser/Parser.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Parser/Parser.cs" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -54,6 +49,7 @@
<setting file="file://$PROJECT_DIR$/Parser/Nodes/ProgramNode.cs" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Parser/Nodes/ProgramNode.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Nodes/FunctionNode.cs" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Parser/Nodes/FunctionNode.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Nodes/ConstantNode.cs" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Parser/Nodes/ConstantNode.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/87DBB896-6B20-4A14-9B6A-7AB43642646D/bb/d4cc6d05/List`1.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Exceptions/MissingSemicolonException.cs" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Parser/Exceptions/MissingSemicolonException.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Exceptions/WrongTypeException.cs" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Parser/Exceptions/WrongTypeException.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Parser.cs" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Parser/Parser.cs" root0="FORCE_HIGHLIGHTING" />
@ -84,8 +80,8 @@
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="ASKED_ADD_EXTERNAL_FILES" value="true" /> <property name="ASKED_ADD_EXTERNAL_FILES" value="true" />
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> <property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="DebuggerViewTab_PTCS_FirstProportionKey" value="0.5698324" /> <property name="DebuggerViewTab_PTCS_FirstProportionKey" value="0.34034768" />
<property name="DebuggerViewTab_PTCS_LastProportionKey" value="0.48789573" /> <property name="DebuggerViewTab_PTCS_LastProportionKey" value="0.0" />
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> <property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> <property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
@ -93,7 +89,7 @@
<property name="XThreadsFramesViewSplitterKey" value="0.44757032" /> <property name="XThreadsFramesViewSplitterKey" value="0.44757032" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/.." /> <property name="last_opened_file_path" value="$PROJECT_DIR$/.." />
<property name="nodejs_package_manager_path" value="npm" /> <property name="nodejs_package_manager_path" value="npm" />
<property name="settings.editor.selected.configurable" value="preferences.pluginManager" /> <property name="settings.editor.selected.configurable" value="codetogether.settings" />
<property name="vue.rearranger.settings.migration" value="true" /> <property name="vue.rearranger.settings.migration" value="true" />
</component> </component>
<component name="RunManager"> <component name="RunManager">
@ -142,8 +138,17 @@
<workItem from="1598260697761" duration="4427000" /> <workItem from="1598260697761" duration="4427000" />
<workItem from="1598873360208" duration="3527000" /> <workItem from="1598873360208" duration="3527000" />
<workItem from="1608233616774" duration="1881000" /> <workItem from="1608233616774" duration="1881000" />
<workItem from="1608235612613" duration="1783000" /> <workItem from="1608235612613" duration="6721000" />
<workItem from="1608242348552" duration="4027000" />
</task> </task>
<task id="LOCAL-00001" summary="Lots of comments and cleanup">
<created>1608237399848</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1608237399848</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -166,8 +171,53 @@
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" /> <option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" /> <option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
<MESSAGE value="Lots of comments and cleanup" />
<option name="LAST_COMMIT_MESSAGE" value="Lots of comments and cleanup" />
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="DotNet Breakpoints">
<url>file://$PROJECT_DIR$/DevFunctions.cs</url>
<line>118</line>
<properties documentPath="$PROJECT_DIR$/DevFunctions.cs" initialLine="118" containingFunctionPresentation="method 'DevMode'">
<startOffsets>
<option value="3680" />
</startOffsets>
<endOffsets>
<option value="3727" />
</endOffsets>
</properties>
<option name="timeStamp" value="1" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DotNet Breakpoints">
<url>file://$PROJECT_DIR$/Parser/Parser.cs</url>
<line>52</line>
<properties documentPath="$PROJECT_DIR$/Parser/Parser.cs" initialLine="52" containingFunctionPresentation="method 'Parse'">
<startOffsets>
<option value="1564" />
</startOffsets>
<endOffsets>
<option value="1593" />
</endOffsets>
</properties>
<option name="timeStamp" value="2" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DotNet Breakpoints">
<url>file://$PROJECT_DIR$/Parser/Parser.cs</url>
<line>154</line>
<properties documentPath="$PROJECT_DIR$/Parser/Parser.cs" initialLine="154" containingFunctionPresentation="method 'ParseExpressionNode'">
<startOffsets>
<option value="4942" />
</startOffsets>
<endOffsets>
<option value="4943" />
</endOffsets>
</properties>
<option name="timeStamp" value="3" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
<pin-to-top-manager> <pin-to-top-manager>
<pinned-members> <pinned-members>
<PinnedItemInfo parentTag="Type#Compiler.Parser.Nodes.Node" memberName="Children" /> <PinnedItemInfo parentTag="Type#Compiler.Parser.Nodes.Node" memberName="Children" />

View file

@ -112,7 +112,7 @@ namespace Compiler
{ {
*/ */
string file = "/home/clemens/repositorys/lcc/stage_3/valid/complicated.c"; string file = "/home/clemens/repositorys/lcc/stage_3/valid/associativity_2.c";
Console.WriteLine("-------------"); Console.WriteLine("-------------");
List<Token> tokens = TestLexer(file, 0); List<Token> tokens = TestLexer(file, 0);

View file

@ -32,7 +32,7 @@ namespace Compiler.Parser
} }
//The main RDP function //The main RDP function
public Node Parse(NodeType nodeType, bool unopPrecedence = false) public Node Parse(NodeType nodeType)
{ {
//switch over the nodeType to check in which part of the parser we are //switch over the nodeType to check in which part of the parser we are
switch (nodeType) switch (nodeType)
@ -50,18 +50,18 @@ namespace Compiler.Parser
return ParseReturnNode(); return ParseReturnNode();
case NodeType.ExpressionNode: case NodeType.ExpressionNode:
return ParseExpressionNode(unopPrecedence); return ParseExpressionNode();
//the case for term node is almost the same as the one for expression node //the case for term node is almost the same as the one for expression node
case NodeType.TermNode: case NodeType.TermNode:
return ParseTermNode(unopPrecedence); return ParseTermNode();
case NodeType.FactorNode: case NodeType.FactorNode:
return ParseFactorNode(); return ParseFactorNode();
//code to parse unary operator nodes //code to parse unary operator nodes
case NodeType.UnaryOperatorNode: case NodeType.UnaryOperatorNode:
return ParseUnopNode(unopPrecedence); return ParseUnopNode();
//parse constant nodes //parse constant nodes
case NodeType.ConstantNode: case NodeType.ConstantNode:
@ -96,7 +96,7 @@ namespace Compiler.Parser
{ {
throw new MissingTokenException(TokenType.IdentifierToken); throw new MissingTokenException(TokenType.IdentifierToken);
} }
//check if the identifier has a value //check if the identifier has a value
if (_tokenList[0].Value != null) if (_tokenList[0].Value != null)
{ {
@ -151,7 +151,7 @@ namespace Compiler.Parser
return n; return n;
} }
private Node ParseExpressionNode(bool unopPrecedence) private Node ParseExpressionNode()
{ {
if (_tokenList.Count == 0) if (_tokenList.Count == 0)
{ {
@ -168,10 +168,16 @@ namespace Compiler.Parser
//if the next token is a + or a - it must be a binary operator because we are in an expression //if the next token is a + or a - it must be a binary operator because we are in an expression
//that means that this is not a plain constant but a BinaryOperator node //that means that this is not a plain constant but a BinaryOperator node
//also check if the last token was a unary operator while (expressionToken.TokenType == TokenType.AdditionToken ||
while ((expressionToken.TokenType == TokenType.AdditionToken || expressionToken.TokenType == TokenType.NegationToken)
expressionToken.TokenType == TokenType.NegationToken) && !unopPrecedence)
{ {
//if this node is already a complete binary expression
//(if this while loop has already ran)
if (n.Children.Count == 2)
{
firstTerm = n;
}
//remove the - or + token //remove the - or + token
_tokenList.RemoveAt(0); _tokenList.RemoveAt(0);
switch (expressionToken.TokenType) switch (expressionToken.TokenType)
@ -180,15 +186,22 @@ namespace Compiler.Parser
//2. Add the first term as a child //2. Add the first term as a child
//3. Parse the rest as an expression and add it as the second term //3. Parse the rest as an expression and add it as the second term
case TokenType.AdditionToken: case TokenType.AdditionToken:
Node secondTerm = Parse(NodeType.TermNode);
n = new BinaryOperatorNode(OperatorType.Addition); n = new BinaryOperatorNode(OperatorType.Addition);
n.Children.Add(firstTerm); n.Children.Add(firstTerm);
n.Children.Add(Parse(NodeType.ExpressionNode)); n.Children.Add(secondTerm);
break; break;
case TokenType.NegationToken: case TokenType.NegationToken:
secondTerm = Parse(NodeType.TermNode);
n = new BinaryOperatorNode(OperatorType.Subtraction); n = new BinaryOperatorNode(OperatorType.Subtraction);
n.Children.Add(firstTerm); n.Children.Add(firstTerm);
n.Children.Add(Parse(NodeType.ExpressionNode)); n.Children.Add(secondTerm);
break; break;
default: default:
//this should never happen because the while loop checks for token types, that are //this should never happen because the while loop checks for token types, that are
//handled by case statements above only //handled by case statements above only
@ -210,7 +223,7 @@ namespace Compiler.Parser
return n; return n;
} }
private Node ParseTermNode(bool unopPrecedence) private Node ParseTermNode()
{ {
if (_tokenList.Count == 0) if (_tokenList.Count == 0)
{ {
@ -225,22 +238,42 @@ namespace Compiler.Parser
//parse second factor if it exists //parse second factor if it exists
while ((termToken.TokenType == TokenType.MultiplicationToken || while ((termToken.TokenType == TokenType.MultiplicationToken ||
termToken.TokenType == TokenType.DivisionToken) && !unopPrecedence) termToken.TokenType == TokenType.DivisionToken))
{ {
//if this node is already a complete binary expression
//(if this while loop has already ran)
if (n.Children.Count == 2)
{
//switch n to be the first factor so precedence is kept
firstFactor = n;
}
//check if the next token is a * or an /
_tokenList.RemoveAt(0); _tokenList.RemoveAt(0);
switch (termToken.TokenType) switch (termToken.TokenType)
{ {
//if the next operation is multiplication
case TokenType.MultiplicationToken: case TokenType.MultiplicationToken:
//create a new multiplication node
n = new BinaryOperatorNode(OperatorType.Multiplication); n = new BinaryOperatorNode(OperatorType.Multiplication);
//add the first factor
n.Children.Add(firstFactor); n.Children.Add(firstFactor);
n.Children.Add(Parse(NodeType.TermNode)); //parse and add the second factor
Node secondFactor = Parse(NodeType.FactorNode);
n.Children.Add(secondFactor);
break; break;
//if the next operation is division
case TokenType.DivisionToken: case TokenType.DivisionToken:
//create a new division node
n = new BinaryOperatorNode(OperatorType.Division); n = new BinaryOperatorNode(OperatorType.Division);
//add the first child
n.Children.Add(firstFactor); n.Children.Add(firstFactor);
n.Children.Add(Parse(NodeType.TermNode)); //add the second child
secondFactor = Parse(NodeType.FactorNode);
n.Children.Add(secondFactor);
break; break;
//This should never happen since the while loop checks
default: default:
throw new Exception("WeirdException"); throw new Exception("WeirdException");
} }
@ -303,7 +336,7 @@ namespace Compiler.Parser
return n; return n;
} }
private Node ParseUnopNode(bool unopPrecedence) private Node ParseUnopNode()
{ {
if (_tokenList.Count == 0) if (_tokenList.Count == 0)
{ {
@ -321,15 +354,15 @@ namespace Compiler.Parser
{ {
case TokenType.BitwiseComplementToken: case TokenType.BitwiseComplementToken:
n = new UnaryOperatorNode(OperatorType.BitwiseComplement); n = new UnaryOperatorNode(OperatorType.BitwiseComplement);
n.Children.Add(Parse(NodeType.ExpressionNode, unopPrecedence = true)); n.Children.Add(Parse(NodeType.FactorNode));
break; break;
case TokenType.NegationToken: case TokenType.NegationToken:
n = new UnaryOperatorNode(OperatorType.Negation); n = new UnaryOperatorNode(OperatorType.Negation);
n.Children.Add(Parse(NodeType.ExpressionNode, unopPrecedence = true)); n.Children.Add(Parse(NodeType.FactorNode));
break; break;
case TokenType.LogicalNegationToken: case TokenType.LogicalNegationToken:
n = new UnaryOperatorNode(OperatorType.LogicalNegation); n = new UnaryOperatorNode(OperatorType.LogicalNegation);
n.Children.Add(Parse(NodeType.ExpressionNode, unopPrecedence = true)); n.Children.Add(Parse(NodeType.FactorNode));
break; break;
default: default:
throw new UnexpectedTokenException(TokenType.IntegerLiteralToken, throw new UnexpectedTokenException(TokenType.IntegerLiteralToken,