Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a parser and a scanner?

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

like image 647
neuromancer Avatar asked Nov 15 '09 22:11

neuromancer


People also ask

How do parser and scanner communicate?

In the traditional arrangement, the parser calls the scanner whenever it needs a token. That's the same logic as used in the scanner (or many other programs) which call the I/O library every time they need more input.

What is a parser used for?

Parsers are used when there is a need to represent input data from source code abstractly as a data structure so that it can be checked for the correct syntax. Coding languages and other technologies use parsing of some type for this purpose.

What is a scanner in a compiler?

SUMMARY. The scanner is a subroutine which is frequently called by an application program like a compiler. The primary function of a scanner is to combine characters from the input stream into recognizable units called tokens.

What is the difference between a parser and a compiler?

A parser just reads a text into an internal, more abstract representation, often a tree or graph of some sort. A compiler translates such an internal representation into another format. Most often this means converting source code into executable programs. But the target doesn't have to be machine code.


2 Answers

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).

like image 150
barkmadley Avatar answered Oct 19 '22 23:10

barkmadley


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.

like image 33
Chris Tonkinson Avatar answered Oct 19 '22 23:10

Chris Tonkinson