0
$\begingroup$

In some languages, it is expected that a function declaration be terminated by syntax that includes the function name. For example, in MODULA-2, a function is declared as shown below:

PROCEDURE P ;

BEGIN

...

END P ;

Is it possible to verify such a requirement using a syntax analyzer? If yes, explain how to do it. If not, provide an alternative explanation.

From what I understand, this part should be the job of semantic analysis, something like verifying the scope of variables, particularly that of "P," and in general, I believe the answer should be no. Well, I don't see how in the process of syntax analysis, this part can be verified. For example, if we generate the syntax analysis tree or parse tree, one way we could verify that the termination of the function is correct is by ensuring that there are at least 2 derivations to reach the leaves where "P" would be, but this wouldn't necessarily indicate that this is correct. For instance, if there were recursion or another call to "P" in the code block, the idea wouldn't work.

Any advice?

and well jeje,and ... how would the syntax analysis tree be? I've tried to do it, but what I'm doing doesn't make much sense. Any ideas on how I could guide myself to do it?"

thanks for your time

$\endgroup$

2 Answers 2

3
$\begingroup$

It depends on what is meant by a syntax analyzer.

Generally, in computer science, different people often mean different things by the same term, so always consult the definition you were given.

It is very easy for a syntax analyzer to do this. Let's assume it is based on a parser that uses a well-known parsing technique for parsing a context-free grammar for the language.

The grammar will probably have a rule similar to this:

<procedure definition> :==
  PROCEDURE <name>;
  BEGIN
  <procedure definition body>
  END <name>;

The grammar is context-free, so it cannot express the fact that the two <name>s here must expand to the same name. What you really need is something like this:

<procedure definition> :==
  PROCEDURE <name(p1)>;
  BEGIN
  <procedure definition body>
  END <name(p2)>;
  <where(p1 == p2)>

Any sane parser generator based on context-free grammars will allow you to do this, but the <where ...> clause may not be part of the grammar; you may need to check that condition by writing code in the programming language you're using.

It would be easier if the grammar formalism itself allowed you to do this. That would make something like the above a valid grammar in that formalism. Such grammar formalisms exist; they are called attribute grammars, or affix grammars, or Van Wijngaarden grammars, and no doubt some other names for them exist. But they are not taught in all compiler construction causes. So look at your course notes and textbook and base your answer on them.

Another question is whether to call this requirement a syntactic property and its analysis syntactic analysis. Many people would call it semantics rather than syntax. Once again, this is a matter of definition.

$\endgroup$
3
  • $\begingroup$ Newlines are nowhere required in Modula. No even blanks. $\endgroup$
    – user16034
    Commented Sep 11, 2023 at 10:31
  • $\begingroup$ Thanks; fixed. It's been a while (1983) since I used Modula 2 ... $\endgroup$ Commented Sep 11, 2023 at 10:34
  • $\begingroup$ I worked a lot in Pascal at the time. Then came C and I died. $\endgroup$
    – user16034
    Commented Sep 11, 2023 at 10:39
2
$\begingroup$

Nope,

PROCEDURE P;
BEGIN
END Q;

is syntactically valid. A BNF description cannot express the equality of the identifiers.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.