mirror of
https://github.com/Ed94/LangStudies.git
synced 2024-11-10 04:14:53 -08:00
93 lines
1.2 KiB
Plaintext
93 lines
1.2 KiB
Plaintext
|
Following the first lecture of "Building a Parser from scratch"
|
||
|
By Dmitry Soshnikov.
|
||
|
|
||
|
|
||
|
Lecture 1:
|
||
|
|
||
|
|
||
|
Phases:
|
||
|
|
||
|
Data - Text Content
|
||
|
Processor - Tokenizer
|
||
|
Data - Tokens
|
||
|
Processor - Parser
|
||
|
Data - AST
|
||
|
|
||
|
|
||
|
Example of syntaxes :
|
||
|
|
||
|
S-Expression :
|
||
|
|
||
|
(class Point
|
||
|
(begin
|
||
|
|
||
|
(def constructor (self x y)
|
||
|
(begin
|
||
|
(set (prop self x) x)
|
||
|
(set (prop self y) y)
|
||
|
)
|
||
|
)
|
||
|
|
||
|
(def calc (self)
|
||
|
(+ (prop self x)
|
||
|
(prop self y)
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
|
||
|
(var p (new Point 10 20))
|
||
|
|
||
|
((prop p calc) p)
|
||
|
|
||
|
|
||
|
User Syntax :
|
||
|
|
||
|
class Point
|
||
|
{
|
||
|
def constructor( x, y )
|
||
|
{
|
||
|
this.x = x;
|
||
|
this.y = y;
|
||
|
}
|
||
|
|
||
|
def calc() {
|
||
|
return this.x + this.y;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let
|
||
|
p = new Point(10, 20);
|
||
|
p.calc();
|
||
|
|
||
|
|
||
|
Tokenizer - Lexial Analysis : Uses Regular Expressions (Optimal)
|
||
|
Parser - Syntactic Analysis : Uses Backus-Naur Form
|
||
|
|
||
|
|
||
|
Backus-Naur Example :
|
||
|
|
||
|
Program
|
||
|
: StatementList
|
||
|
;
|
||
|
|
||
|
StatementList
|
||
|
: BlockStatement
|
||
|
| IfStatement
|
||
|
| FunctionDeclaration
|
||
|
...
|
||
|
;
|
||
|
|
||
|
FunctionDeclaration
|
||
|
: def Identifier ( Arguments ) BlockStatement
|
||
|
;
|
||
|
|
||
|
|
||
|
Hand-written parsers :
|
||
|
Use recursive descent.
|
||
|
|
||
|
Automatically generated
|
||
|
All kinds of stuff...
|
||
|
|
||
|
|