comments for parser

This commit is contained in:
Clemens-Dautermann 2020-08-23 18:30:00 +02:00
parent 5744b93eb1
commit 17e2f0e9e3
3 changed files with 91 additions and 73 deletions

View file

@ -20,34 +20,9 @@
</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="">
<change afterPath="$PROJECT_DIR$/Parser/Nodes/BinaryOperatorNode.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/invalid/malformed_paren.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/invalid/missing_first_op.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/invalid/missing_second_op.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/invalid/no_semicolon.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/add.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/associativity.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/associativity_2.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/div.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/div_neg.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/mult.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/parens.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/precedence.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/sub.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/sub_neg.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/unop_add.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../stage_3/valid/unop_parens.c" 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/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$/Lexer/Lexer.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Lexer/Lexer.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Lexer/TokenType.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Lexer/TokenType.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Parser/Nodes/ConstantNode.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Parser/Nodes/ConstantNode.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Parser/Nodes/NodeType.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Parser/Nodes/NodeType.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Parser/Nodes/OperatorType.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Parser/Nodes/OperatorType.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" />
<change beforePath="$PROJECT_DIR$/../grammar" beforeDir="false" afterPath="$PROJECT_DIR$/../grammar" afterDir="false" /> <change beforePath="$PROJECT_DIR$/../grammar" beforeDir="false" afterPath="$PROJECT_DIR$/../grammar" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../test" beforeDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -67,8 +42,7 @@
<component name="HighlightingSettingsPerFile"> <component name="HighlightingSettingsPerFile">
<setting file="file://$PROJECT_DIR$/Compiler.cs" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Compiler.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/714153FB-3DE5-4537-8A59-8AE8F7F7655E/44/126d3750/String.cs" root0="SKIP_HIGHLIGHTING" /> <setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/714153FB-3DE5-4537-8A59-8AE8F7F7655E/44/126d3750/String.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///Dummy.cs" root0="SKIP_HIGHLIGHTING" /> <setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/714153FB-3DE5-4537-8A59-8AE8F7F7655E/46/638f55dc/List`1.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///Dummy.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Lexer/Lexer.cs" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Lexer/Lexer.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Lexer/Token.cs" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Lexer/Token.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/EEDBFCB1-C56E-4F7A-8B60-B8DD27E0DC7D/86/71102cca/Enumerable.cs" root0="SKIP_HIGHLIGHTING" /> <setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/EEDBFCB1-C56E-4F7A-8B60-B8DD27E0DC7D/86/71102cca/Enumerable.cs" root0="SKIP_HIGHLIGHTING" />
@ -130,9 +104,9 @@
<option value="$PROJECT_DIR$/Parser/Nodes/NodeType.cs" /> <option value="$PROJECT_DIR$/Parser/Nodes/NodeType.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/BinaryOperatorNode.cs" /> <option value="$PROJECT_DIR$/Parser/Nodes/BinaryOperatorNode.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/OperatorType.cs" /> <option value="$PROJECT_DIR$/Parser/Nodes/OperatorType.cs" />
<option value="$PROJECT_DIR$/../grammar" />
<option value="$PROJECT_DIR$/DevFunctions.cs" /> <option value="$PROJECT_DIR$/DevFunctions.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/ConstantNode.cs" /> <option value="$PROJECT_DIR$/Parser/Nodes/ConstantNode.cs" />
<option value="$PROJECT_DIR$/../grammar" />
<option value="$PROJECT_DIR$/Parser/Parser.cs" /> <option value="$PROJECT_DIR$/Parser/Parser.cs" />
</list> </list>
</option> </option>
@ -200,6 +174,7 @@
<workItem from="1597952695048" duration="9233000" /> <workItem from="1597952695048" duration="9233000" />
<workItem from="1598037838306" duration="329000" /> <workItem from="1598037838306" duration="329000" />
<workItem from="1598123734996" duration="12557000" /> <workItem from="1598123734996" duration="12557000" />
<workItem from="1598197309733" duration="2871000" />
</task> </task>
<servers /> <servers />
</component> </component>
@ -257,70 +232,70 @@
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.-1.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597955238557" /> <state width="1868" height="383" key="GridCell.Tab.-1.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597955238557" />
<state width="1292" height="330" key="GridCell.Tab.0.bottom" timestamp="1598136287234"> <state width="1292" height="249" key="GridCell.Tab.0.bottom" timestamp="1598199247638">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.0.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287234" /> <state width="1292" height="249" key="GridCell.Tab.0.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199247638" />
<state width="1292" height="330" key="GridCell.Tab.0.center" timestamp="1598136287234"> <state width="1292" height="249" key="GridCell.Tab.0.center" timestamp="1598199247638">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.0.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287234" /> <state width="1292" height="249" key="GridCell.Tab.0.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199247638" />
<state width="1292" height="330" key="GridCell.Tab.0.left" timestamp="1598136287234"> <state width="1292" height="249" key="GridCell.Tab.0.left" timestamp="1598199247638">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.0.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287234" /> <state width="1292" height="249" key="GridCell.Tab.0.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199247638" />
<state width="1292" height="330" key="GridCell.Tab.0.right" timestamp="1598136287234"> <state width="1292" height="249" key="GridCell.Tab.0.right" timestamp="1598199247638">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.0.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287234" /> <state width="1292" height="249" key="GridCell.Tab.0.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199247638" />
<state width="1292" height="330" key="GridCell.Tab.1.bottom" timestamp="1598136287234"> <state width="1292" height="330" key="GridCell.Tab.1.bottom" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.1.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287234" /> <state width="1292" height="330" key="GridCell.Tab.1.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.1.center" timestamp="1598136287234"> <state width="1292" height="330" key="GridCell.Tab.1.center" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.1.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287234" /> <state width="1292" height="330" key="GridCell.Tab.1.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.1.left" timestamp="1598136287234"> <state width="1292" height="330" key="GridCell.Tab.1.left" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.1.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287234" /> <state width="1292" height="330" key="GridCell.Tab.1.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.1.right" timestamp="1598136287234"> <state width="1292" height="330" key="GridCell.Tab.1.right" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.1.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287234" /> <state width="1292" height="330" key="GridCell.Tab.1.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.2.bottom" timestamp="1598136287235"> <state width="1292" height="330" key="GridCell.Tab.2.bottom" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.2.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287235" /> <state width="1292" height="330" key="GridCell.Tab.2.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.2.center" timestamp="1598136287235"> <state width="1292" height="330" key="GridCell.Tab.2.center" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.2.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287235" /> <state width="1292" height="330" key="GridCell.Tab.2.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.2.left" timestamp="1598136287234"> <state width="1292" height="330" key="GridCell.Tab.2.left" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.2.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287234" /> <state width="1292" height="330" key="GridCell.Tab.2.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.2.right" timestamp="1598136287235"> <state width="1292" height="330" key="GridCell.Tab.2.right" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.2.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287235" /> <state width="1292" height="330" key="GridCell.Tab.2.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.3.bottom" timestamp="1598136287235"> <state width="1292" height="330" key="GridCell.Tab.3.bottom" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.3.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287235" /> <state width="1292" height="330" key="GridCell.Tab.3.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.3.center" timestamp="1598136287235"> <state width="1292" height="330" key="GridCell.Tab.3.center" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.3.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287235" /> <state width="1292" height="330" key="GridCell.Tab.3.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.3.left" timestamp="1598136287235"> <state width="1292" height="330" key="GridCell.Tab.3.left" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.3.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287235" /> <state width="1292" height="330" key="GridCell.Tab.3.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1292" height="330" key="GridCell.Tab.3.right" timestamp="1598136287235"> <state width="1292" height="330" key="GridCell.Tab.3.right" timestamp="1598199019855">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1292" height="330" key="GridCell.Tab.3.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598136287235" /> <state width="1292" height="330" key="GridCell.Tab.3.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199019855" />
<state width="1868" height="383" key="GridCell.Tab.4.bottom" timestamp="1597955234640"> <state width="1868" height="383" key="GridCell.Tab.4.bottom" timestamp="1597955234640">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
@ -361,10 +336,10 @@
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state x="2182" y="199" width="432" height="650" key="com.intellij.openapi.editor.actions.MultiplePasteAction$ClipboardContentChooser/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597510086756" /> <state x="2182" y="199" width="432" height="650" key="com.intellij.openapi.editor.actions.MultiplePasteAction$ClipboardContentChooser/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597510086756" />
<state x="3264" y="0" width="576" height="1080" key="dock-window-1" timestamp="1598127152431"> <state x="3264" y="0" width="576" height="1080" key="dock-window-1" timestamp="1598199648208">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state x="3264" y="0" width="576" height="1080" key="dock-window-1/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598127152431" /> <state x="3264" y="0" width="576" height="1080" key="dock-window-1/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1598199648208" />
<state x="3200" y="0" width="640" height="1080" key="dock-window-2" timestamp="1597959520918"> <state x="3200" y="0" width="640" height="1080" key="dock-window-2" timestamp="1597959520918">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
@ -396,18 +371,23 @@
</line-breakpoint> </line-breakpoint>
<line-breakpoint enabled="true" type="DotNet Breakpoints"> <line-breakpoint enabled="true" type="DotNet Breakpoints">
<url>file://$PROJECT_DIR$/Parser/Parser.cs</url> <url>file://$PROJECT_DIR$/Parser/Parser.cs</url>
<line>107</line> <line>261</line>
<properties documentPath="$PROJECT_DIR$/Parser/Parser.cs" initialLine="107"> <properties documentPath="$PROJECT_DIR$/Parser/Parser.cs" initialLine="227">
<startOffsets> <startOffsets>
<option value="3498" /> <option value="10821" />
</startOffsets> </startOffsets>
<endOffsets> <endOffsets>
<option value="3524" /> <option value="10847" />
</endOffsets> </endOffsets>
</properties> </properties>
<option name="timeStamp" value="12" /> <option name="timeStamp" value="13" />
</line-breakpoint> </line-breakpoint>
</breakpoints> </breakpoints>
</breakpoint-manager> </breakpoint-manager>
<pin-to-top-manager>
<pinned-members>
<PinnedItemInfo parentTag="Type#Compiler.Parser.Nodes.Node" memberName="Children" />
</pinned-members>
</pin-to-top-manager>
</component> </component>
</project> </project>

View file

@ -15,6 +15,7 @@ namespace Compiler.Parser
_tokenList = tokenList; _tokenList = tokenList;
} }
//A method to check the first token in _tokenList and remove it or raise accoarding error
private void CheckFirstTokenAndRemove(TokenType expected) private void CheckFirstTokenAndRemove(TokenType expected)
{ {
if (_tokenList.Count == 0) if (_tokenList.Count == 0)
@ -30,9 +31,13 @@ namespace Compiler.Parser
_tokenList.RemoveAt(0); _tokenList.RemoveAt(0);
} }
//The main RDP function
public Node Parse(NodeType nodeType) public Node Parse(NodeType nodeType)
{ {
//declare node to be returned
Node n; Node n;
//switch over the nodeType to check in which part of the parser we are
switch (nodeType) switch (nodeType)
{ {
case NodeType.ProgramNode: case NodeType.ProgramNode:
@ -110,43 +115,57 @@ namespace Compiler.Parser
throw new MissingTokenException(TokenType.IntegerLiteralToken); throw new MissingTokenException(TokenType.IntegerLiteralToken);
} }
//an expression always has a first term
//this first term is parsed here
Node firstTerm = Parse(NodeType.TermNode); Node firstTerm = Parse(NodeType.TermNode);
n = firstTerm; n = firstTerm;
//get next token
Token expressionToken = _tokenList[0]; Token expressionToken = _tokenList[0];
//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
while (expressionToken.TokenType == TokenType.AdditionToken || while (expressionToken.TokenType == TokenType.AdditionToken ||
expressionToken.TokenType == TokenType.NegationToken) expressionToken.TokenType == TokenType.NegationToken)
{ {
//remove the - or + token
_tokenList.RemoveAt(0); _tokenList.RemoveAt(0);
switch (expressionToken.TokenType) switch (expressionToken.TokenType)
{ {
//1. create a BinOp Node
//2. Add the first term as a child
//3. Parse the rest as an expression and add it as the second term
case TokenType.AdditionToken: case TokenType.AdditionToken:
n = new BinaryOperatorNode(OperatorType.Addition); n = new BinaryOperatorNode(OperatorType.Addition);
n.Children.Add(firstTerm); n.Children.Add(firstTerm);
n.Children.Add(Parse(NodeType.TermNode)); n.Children.Add(Parse(NodeType.ExpressionNode));
break; break;
case TokenType.NegationToken: case TokenType.NegationToken:
n = new BinaryOperatorNode(OperatorType.Subtraction); n = new BinaryOperatorNode(OperatorType.Subtraction);
n.Children.Add(firstTerm); n.Children.Add(firstTerm);
n.Children.Add(Parse(NodeType.TermNode)); n.Children.Add(Parse(NodeType.ExpressionNode));
break; break;
default: default:
//this should never happen because the while loop checks for token types, that are
//handled by case statements above only
throw new Exception("WeirdException"); throw new Exception("WeirdException");
} }
//if there are still tokens left over pop one off
if (_tokenList.Count > 0) if (_tokenList.Count > 0)
{ {
expressionToken = _tokenList[0]; expressionToken = _tokenList[0];
} }
else else
{ {
//there must be tokens left because we are in an expression.
throw new MissingTokenException(TokenType.IntegerLiteralToken); throw new MissingTokenException(TokenType.IntegerLiteralToken);
} }
} }
break; break;
//the case for term node is almost the same as the one for expression node
case NodeType.TermNode: case NodeType.TermNode:
@ -155,11 +174,13 @@ namespace Compiler.Parser
throw new MissingTokenException(TokenType.IntegerLiteralToken); throw new MissingTokenException(TokenType.IntegerLiteralToken);
} }
//parse first factor
Node firstFactor = Parse(NodeType.FactorNode); Node firstFactor = Parse(NodeType.FactorNode);
n = firstFactor; n = firstFactor;
Token termToken = _tokenList[0]; Token termToken = _tokenList[0];
//parse second factor if it exists
while (termToken.TokenType == TokenType.MultiplicationToken || while (termToken.TokenType == TokenType.MultiplicationToken ||
termToken.TokenType == TokenType.DivisionToken) termToken.TokenType == TokenType.DivisionToken)
{ {
@ -169,13 +190,13 @@ namespace Compiler.Parser
case TokenType.MultiplicationToken: case TokenType.MultiplicationToken:
n = new BinaryOperatorNode(OperatorType.Multiplication); n = new BinaryOperatorNode(OperatorType.Multiplication);
n.Children.Add(firstFactor); n.Children.Add(firstFactor);
n.Children.Add(Parse(NodeType.FactorNode)); n.Children.Add(Parse(NodeType.TermNode));
break; break;
case TokenType.DivisionToken: case TokenType.DivisionToken:
n = new BinaryOperatorNode(OperatorType.Division); n = new BinaryOperatorNode(OperatorType.Division);
n.Children.Add(firstFactor); n.Children.Add(firstFactor);
n.Children.Add(Parse(NodeType.FactorNode)); n.Children.Add(Parse(NodeType.TermNode));
break; break;
default: default:
throw new Exception("WeirdException"); throw new Exception("WeirdException");
@ -193,6 +214,7 @@ namespace Compiler.Parser
break; break;
case NodeType.FactorNode: case NodeType.FactorNode:
if (_tokenList.Count == 0) if (_tokenList.Count == 0)
{ {
@ -201,20 +223,31 @@ namespace Compiler.Parser
Token factorToken = _tokenList[0]; Token factorToken = _tokenList[0];
//switch over possible next tokens. There are three possible options
switch (factorToken.TokenType) switch (factorToken.TokenType)
{ {
//first option:
//a parenthesised expression follows.
case TokenType.OpenParenthesisToken: case TokenType.OpenParenthesisToken:
//check and remove openParenthesis (it has already been checked in the case, so removal would be enough)
CheckFirstTokenAndRemove(TokenType.OpenParenthesisToken); CheckFirstTokenAndRemove(TokenType.OpenParenthesisToken);
//parse the things inside as an expression
n = Parse(NodeType.ExpressionNode); n = Parse(NodeType.ExpressionNode);
//check if close parenthesis is supplied
CheckFirstTokenAndRemove(TokenType.CloseParenthesisToken); CheckFirstTokenAndRemove(TokenType.CloseParenthesisToken);
break; break;
//second option:
//this is a unary operator expression
case TokenType.NegationToken: case TokenType.NegationToken:
case TokenType.BitwiseComplementToken: case TokenType.BitwiseComplementToken:
case TokenType.LogicalNegationToken: case TokenType.LogicalNegationToken:
//just parse this as a unary operator node
n = Parse(NodeType.UnaryOperatorNode); n = Parse(NodeType.UnaryOperatorNode);
n.Children.Add(Parse(NodeType.FactorNode));
break; break;
//this is an integer literal.
case TokenType.IntegerLiteralToken: case TokenType.IntegerLiteralToken:
//parse it as a constant
n = Parse(NodeType.ConstantNode); n = Parse(NodeType.ConstantNode);
break; break;
default: default:
@ -224,6 +257,7 @@ namespace Compiler.Parser
break; break;
//code to parse unary operator nodes
case NodeType.UnaryOperatorNode: case NodeType.UnaryOperatorNode:
if (_tokenList.Count == 0) if (_tokenList.Count == 0)
{ {
@ -231,9 +265,11 @@ namespace Compiler.Parser
} }
else else
{ {
//get operator
Token unaryOperator = _tokenList[0]; Token unaryOperator = _tokenList[0];
_tokenList.RemoveAt(0); _tokenList.RemoveAt(0);
//switch over three different operators and parse the rest as an expression
switch (unaryOperator.TokenType) switch (unaryOperator.TokenType)
{ {
case TokenType.BitwiseComplementToken: case TokenType.BitwiseComplementToken:
@ -256,6 +292,7 @@ namespace Compiler.Parser
break; break;
//parse constant nodes
case NodeType.ConstantNode: case NodeType.ConstantNode:
if (_tokenList.Count == 0) if (_tokenList.Count == 0)
@ -276,6 +313,7 @@ namespace Compiler.Parser
break; break;
} }
//default case if the supplied NodeType is unknown
default: default:
throw new Exception("Unknown Node Type " + nodeType); throw new Exception("Unknown Node Type " + nodeType);
} }

View file

@ -7,4 +7,4 @@
<unary_op> ::= "!" | "~" | "-" <unary_op> ::= "!" | "~" | "-"
------------ ------------
6+5+2*(3-2)+9 6+5+2*(3-2)+~9