Identifying Phrase Structure

In elementary school, we all learned (and probably forgot) how to identify the parts of speech in a sentence like verb and noun. We can do the same thing with computer languages (we call it syntax analysis). Vocabulary symbols (tokens) play different roles like variable and operator. We can even identify the role of token subsequences like expression.

Take return x+1;, for example. Sequence x+1 plays the role of an expression and the entire phrase is a return statement, which is also a kind of statement. If we represent that visually, we get a sentence diagram of sorts:

images/parsing/return.png

Flip that over, and you get what we call a parse ...

Get Language Implementation Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.