-1

I have a language that describe a domain by predicates of arbitrary arity. For example, in first-order logic I can write the following rule:

PersonlivesInCountry(x,y) <-> person(x), PersonlivesInState(x,z), StateisInCountry(z,y)

In this exampple, all the pair in PersonLivesInCountry are exactly those who match the other restrictions. The key point here is to express the join between the other restriction, for example the z in common with PersonLivesInState and StateIsInCountry predicates. In my language, I always have this structure so I need to write a grammar to express this join condition. Is it possible to ensure that each exit index of a relation is in a (inner) join with the successor?

Wall
  • 293
  • 3
  • 13
  • What do you mean by "express the join between the other restriction", "each exit index of a relation" & "is in a (inner) join with the successor"? Use enough words, sentences & references to parts of examples to be clear. Which of many relational algebras are you mapping to? What exactly are you doing & why is there a problem doing it? PS Assuming relation p holds the rows satisfying p(...), we would expect `PersonlivesInCountry(x,y) <-> person(x), PersonlivesInState(x,z), StateisInCountry(z,y)` iff `PersonlivesInCountry = person natural join PersonlivesInState natural join StateisInCountry`. – philipxy Dec 06 '18 at 00:36

1 Answers1

1

Context-free grammars cannot express name agreement (unless the set of possible names is finite, and even then it's a pain).

That sort of analysis generally falls into the category of "semantic analysis", which is usually easier to write as a (series of) walks over the abstract syntax tree (AST). In that model, the parser just builds the AST.

For simple parsers, semantic analysis can be done during the parse, by attaching semantic actions to each syntactic rule. However, this often leads to badly-factored code which is harder to maintain and modify in the long run.

That said, it is not uncommon to see semantic actions used not just to build an AST but also a symbol table. I'm not as fond of this as I used to be; these days I limit symbol analysis in the parser to entering the symbol in a table of interned symbols, which simplifies memory management and speeds up symbol lookup during semantic analysis. YMMV. Good luck with the project.

rici
  • 234,347
  • 28
  • 237
  • 341