logoalt Hacker News

Jtsummers • today at 4:38 AM • 1 reply • view on HN

> But there's not really a concept of 'actions' to go with parsing.

I'll appeal to authority here and quote from the Dragon Book:

>> Program fragments embedded within production bodies are called semantic actions.

In CS, particularly around compilers, it's an old concept.


Replies

thaumasiotes • today at 5:10 AM

Well, quoting from the Dragon Book, page 8:

--- begin quote ---

The second phase of the compiler is syntax analysis or parsing. The parser uses the first components of the tokens produced by the lexical analyzer to create a tree-like intermediate representation that depicts the grammatical structure of the token stream. A typical representation is a syntax tree in which each interior node represents an operation and the children of the node represent the arguments of the operation. A syntax tree for the token stream (1.2) is shown as the output of the syntactic analyzer in Fig. 1.7.

This tree shows the order in which the operations in the assignment

    position = initial + rate * 60
are to be performed.

--- end quote ---

For context, token stream 1.2 is the sequence of lexer output "ID1 = ID2 + ID3 * 60" (7 tokens long), and Figure 1.7 shows that sequence being transformed by a parser into the tree "[= ID1 [+ ID2 [* ID3 60]]]".

Back to the textbook:

--- resume quote ---

The subsequent phases of the compiler use the grammatical structure to help analyze the source program and generate the target program.

--- end quote ---

I'm struggling to see where this disagrees with my earlier comment in even the most minor detail.

I notice that the quote you provide comes from a later chapter, "A Simple Syntax-Directed Translator", and that there is no suggestion there that parsing involves semantic actions. Rather, semantic actions annotated on to a finished parse tree are presented as a tool for generating translations.

Had you moved on to the chapter section immediately following the one you quoted from -- the one titled "Parsing" (page 60) -- you might have seen that it opens by saying this:

Parsing is the process of determining how a string of terminals can be generated by a grammar.

➕ show 2 replies