views:

266

answers:

2

I already made a scanner, now I'm supposed to make a parser. What's the difference?

+3  A: 

A Scanner simply turns an input String (say a file) into a list of tokens. These tokens represent things like identifiers, parentheses, operators etc.

A parser converts this list of tokens into a Tree-like object to represent how the tokens fit together to form a cohesive whole (sometimes referred to as a sentence).

In terms of programming language parsers, the output is usually referred to as an Abstract Syntax Tree (AST). Each node in the AST represents a different construct of the language, e.g. an IF statement would be a node with 2 or 3 sub nodes, a CONDITION node, a THEN node and potentially an ELSE node.

A parser does not give the nodes any meaning beyond structural cohesion. The next thing to do is extract meaning from this structure (sometimes called contextual analysis).

barkmadley
+1  A: 

Parsing (in a general sense) is about turning the symbols (characters, digits, left parens, etc) into sentences of your grammar.

The lexical analyzer (the "lexer") parses individual symbols from the source code file into tokens. From there, the "parser" proper turns those whole tokens into sentences of your grammar.

Put another way, the lexer combines symbols into tokens, and the parser combines tokens to form sentences.

Chris