how to choose between identificator or function call?

Apr 13, 2010 at 10:24 AM
Edited Apr 13, 2010 at 10:25 AM

Hi All!!!

Please, i need your advice.

I making parser for pascal-like language, and i don't understand how to choose between identificator or function call? 

that  grammar to use? 

for example:



function b:integer;


  return 1;





       ^---- in this place i have a problem -  i don't know what to do... 




Apr 14, 2010 at 4:55 AM

it is in fact not doable at parsing stage. You should differentiate between syntax analysis - what can be derived from pure phrase structure; and semantics - what follows from language rules and meaning it gives to certain constructs. In your case, "a:=b" would be in fact in most languages interpreted as assignment of "ref to b-function" to variable "a", not a call to function "b()" and assignment of result to a; while expression like "a = b()" is usually interpreted as a function call. So you parse all these a's and b's as identifiers, build a parse tree/AST tree, and then at the phase of analysis (scanning AST tree) you derive more information - that "b" is in fact ref to function, a is a variable, etc

You can find more on this in compiler/parsing textbooks, or even in wikipedia entries for syntax analysis and symantic analysis.