0
votes

I am confused as to whether the following is allowed:

(I am using declaration in the forloop rule however declaration also defines how to declare other things. Could this be error checked later in the compiler? Am I clear?)

declaration :

operand ASSIGNMENTOPERATOR variable var_type CONST?
|operations ASSIGNMENTOPERATOR variable var_type CONST?
|funcall ASSIGNMENTOPERATOR variable var_type CONST?
|(funcall|operand|NOINDEXARRAY) ASSIGNMENTOPERATOR variable var_type ARRAY CONST? ;

forloop :
block
(LPARENS ((number_operation ASSIGNMENTOPERATOR variable)|number_functions)
    SEMICOLON bool_operation
    SEMICOLON declaration
    RPARENS
)
'for'
;

UPDATE: I know that it would work when I supply the right type of declaration inside the for loop. The question is what happens if I don't?

1
Welcome to StackOverflow. Please visit the docs on how to write a question. A good question has a small, runnable example and reflects the work you've done to answer it yourself. Have you tried to compile this with ANTLR? What leads you to think it will or won't work?Gene
Thanks for the comment. I haven't tried compiling it as we will be creating a compiler for this language later. At the moment my task is to define the lexer and parser rules so I can't really try if it works.bluiska
Why not? You can easily compile little chunks of grammar and get the ANTLR parser to generate syntax trees for inputs to verify they're parsing as you expect. In this case just make declaration the start symbol and go! Trying to develop an entire grammar for a language without ever compiling any piecs is likely to waste a lot of your time.Gene

1 Answers

0
votes

It seems what you have in mind is a semantic phase, which is very typical in parser setups. Parsing the input is only a small part of the work. Usually you have a step after that to validate your parse tree (e.g. look for duplicate variable names or unknown symbols and check other conditions). This is usually called the semantic phase (parsing is the syntactic phase).

You can use this semantic phase for all kind of error checking, including your declaration check (whatever you want to check there, that's not clear from your question).