Browse Source

Cleaned grammar to not have trouble with the distinction between IF_NODE and IF, for example. The parser doesn't take care of this completely though...

Yentl Van Tendeloo 9 years ago
parent
commit
877e44c9b1

+ 25 - 22
interface/HUTN/grammars/actionlanguage.g

@@ -5,7 +5,14 @@ grammar{
 
     include: INCLUDE STRVALUE newline+;
 
-    statement: ((vardecl | assignment | return | func_call) newline) | (ifelse | while) | (newline) @Rm;
+    statement 
+        : (vardecl newline)
+        | (assignment newline)
+        | (return newline)
+        | (func_call newline)
+        | ifelse
+        | while
+        | newline @Rm;
 
     vardecl: type_specifier ID;
 
@@ -18,10 +25,6 @@ grammar{
 	binary_operation
 	    :   disjunction;
 
-    //lvalue
-    //    :   IDENTIFIER
-    //    |   IDENTIFIER LSQUARE expression RSQUARE;
-
     disjunction
         :   (disjunction OR conjunction)
         |   conjunction;
@@ -78,7 +81,7 @@ grammar{
     deref: QUESTIONMARK ANYTHING?;
 
     type_specifier: INT | FLOAT | BOOL | STRING | TYPE | ACTION | ELEMENT;
-    actionname: EXCLAMATION (IF_NODE | WHILE_NODE | ASSIGN_NODE | CALL_NODE | BREAK_NODE | CONTINUE_NODE | RETURN_NODE | RESOLVE_NODE | ACCESS_NODE | CONSTANT_NODE | GLOBAL_NODE | DECLARE_NODE | INPUT_NODE | OUTPUT_NODE);
+    actionname: IF_NODE | WHILE_NODE | ASSIGN_NODE | CALL_NODE | BREAK_NODE | CONTINUE_NODE | RETURN_NODE | RESOLVE_NODE | ACCESS_NODE | CONSTANT_NODE | GLOBAL_NODE | DECLARE_NODE | INPUT_NODE | OUTPUT_NODE;
 
     string: (STRVALUE|LONG_STRVALUE);
 
@@ -123,27 +126,25 @@ grammar{
 
     tokens{
         // TOKENS (written in CAPS)
-        ID: '[a-zA-Z_][a-zA-Z_0-9.]*';
-        ANYTHING: '[a-zA-Z_0-9/.]+';
         VOID: 'Void';
         INCLUDE: 'include';
 
         ELEMENT: 'Element';
 
-        IF_NODE: 'if';
-        WHILE_NODE: 'while';
-        ASSIGN_NODE: 'assign';
-        CALL_NODE: 'call';
-        BREAK_NODE: 'break';
-        CONTINUE_NODE: 'continue';
-        RETURN_NODE: 'return';
-        RESOLVE_NODE: 'resolve';
-        ACCESS_NODE: 'access';
-        CONSTANT_NODE: 'constant';
-        INPUT_NODE: 'input';
-        OUTPUT_NODE: 'output';
-        GLOBAL_NODE: 'global';
-        DECLARE_NODE: 'declare';
+        IF_NODE: '!if';
+        WHILE_NODE: '!while';
+        ASSIGN_NODE: '!assign';
+        CALL_NODE: '!call';
+        BREAK_NODE: '!break';
+        CONTINUE_NODE: '!continue';
+        RETURN_NODE: '!return';
+        RESOLVE_NODE: '!resolve';
+        ACCESS_NODE: '!access';
+        CONSTANT_NODE: '!constant';
+        INPUT_NODE: '!input';
+        OUTPUT_NODE: '!output';
+        GLOBAL_NODE: '!global';
+        DECLARE_NODE: '!declare';
 
         FUNCTION: 'function';
         RETURN: 'return';
@@ -203,5 +204,7 @@ grammar{
         FALSE: 'False';
 
         COMM: '[\t]*//[^\n]*';
+        ID: '[a-zA-Z_][a-zA-Z_0-9.]*';
+        ANYTHING: '[a-zA-Z_0-9/.]+';
     }
 }

+ 1 - 1
interface/HUTN/hutn_compiler/constructors_visitor.py

@@ -109,7 +109,7 @@ class ConstructorsVisitor(Visitor):
 
     def visit_actionname(self, tree):
         self.add_constructors('"const"',
-            '{"value": "%s"}' % (tree.get_tail()[1].get_text()))
+            '{"value": "%s"}' % (tree.get_tail()[0].get_text()[1:]))
 
     def visit_string(self, tree):
         self.visit_literal(tree)

+ 3 - 0
interface/HUTN/test/constructor_compilation_action_language/test_compile.py

@@ -67,3 +67,6 @@ class TestCompile(unittest.TestCase):
     def test_factorial(self):
         compile_file(self, "factorial.al")
 
+    def test_strange_return(self):
+        compile_file(self, "strange_return.al")
+