5

Building a Parser from scratch

 3 years ago
source link: http://dmitrysoshnikov.com/courses/parser-from-scratch/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Building a Parser from scratch – Dmitry Soshnikov
Course overview

Parsing or syntactic analysis is one of the first stages in designing and implementing a compiler. A well-designed syntax of your programming language is a big motivation why users would prefer and choose exactly your language.

Note: this is a practical class on building a manual Recursive-descent parser. If you’re interested in parsing theory and automated algorithms you may also consider the [ Parsing Algorithms ] class.

RDP_logo.png

Recursive descent parsers are the group of parsers which are widely used on practice in many production programming languages. In contrast with automated parsing algorithms, the manual implementation allows having full control over the parsing process, and handling complex constructs, which may not be possible in the automatic parsers.

Besides, implementing a full manual parser from scratch allows understanding and seeing this process from inside, demystifying internal structures, and turning building parsers into an interesting engineering task.


In the Building a Parser from scratch class we dive into pure practical implementation, building and learning different aspects of parsers.

In this class you will learn concept of Recursive descent parsing, understand what is Tokenizer and how it cooperates with Parser module, learn what is Abstract Syntax Tree (AST), and how to have different formats of these ASTs, what is “lookahead” and the predictive parsing, and eventually build a parser for a full programming language, similar to Java or JavaScript.

Implementing a parser would also make your practical usage of other programming languages more professional.

RDP_screen_1_small.png
How to?

You can watch preview lectures, and also enroll to the full course, describing implementation of a Recursive descent parser from scratch, in animated and live-annotated format. See details below what is in the course.

Note: use RDP_LAUNCH coupon for the limited time launch discount.

Who this class is for?

This class is for any curious engineer, who would like to gain skills of building complex systems (and building a parser for a programing language is a pretty advanced engineering task!), and obtain a transferable knowledge for building such systems.

If you are interested specifically in compilers, interpreters, and source code transformation tools, then this class is also for you.

The pre-requisites for this class are the basic data structures and algorithms: trees, lists, traversal, and regular expressions.

If you took or going to take class on Building an Interpreter from scratch, the parsers class can be a syntax frontend for the interpreter built in that class.

What is used for implementation?

Since we build a language very similar in syntax to JavaScript or Java we use specifically JavaScript — its elegant multi-paradigm structure which combines functional programming, class-based, and prototype-based OOP fits ideal for that.

Many engineers are familiar with JavaScript so it should be easier to start coding right away. However we do not use very JS-specific constructs, so the implementation of the parser can easily be transferred to any other language of your choice.

Note: we want our students to actually follow, understand and implement every detail of the parser themselves, instead of just copy-pasting from final solution. The full source code for the language is available in video lectures, showing and guiding how to structure specific modules.

What’s specific in this class?
EoI-screen-2.png

The main features of these lectures are:

  • Concise and straight to the point. Each lecture is self-sufficient, concise, and describes information directly related to the topic, not distracting on unrelated materials or talks.
  • Animated presentation combined with live-editing notes. This makes understanding of the topics easier, and shows how (and when at time) the object structures are connected. Static slides simply don’t work for a complex content.
  • Live coding session end-to-end with assignments. The full source code, starting from scratch, and up to the very end is presented in video lectures of the class
What is in the course?

The course is divided into four parts, in total of 18 lectures, and many sub-topics in each lecture. Below is the table of contents and curriculum.

Part 1: Basic expressions and Tokenizer

In this part we describe basic expressions, such as Numbers and Strings, and also build the Tokenizer modules, operating with regular expressions.

RDP_Lecture_1_Intro_cover_small.png
  • Course overview and agenda
  • Parsing pipeline
  • Tokenizer module (Lexical analysis)
  • Parser module (Syntactic analysis)
  • Abstract Syntax Tree (AST)
  • Regular expression notation
  • Backus-Naur form (BNF) notation
  • Grammars and productions
  • Hand-written and Automatic parsers
  • Syntax: language-agnostic parser generator
  • The Letter programming language
  • Numeric literals

Lecture 2: Numbers | Strings
RDP_Lecture_2_Tokenizer_cover_small.png
  • Tokenizer module (Lexical analysis)
  • Number and String tokens
  • Program AST node
  • Lookahead
  • Numeric literals
  • String literals
  • Finite state machine

Lecture 3: From State Machines to Regular Expressions
RDP_Lecture_3_Tokenizer_regexp_cover_small.png
  • Tokenizers as Finite state machines
  • Regular Expressions notation
  • Tokenizer spec
  • Generic getNextToken
  • Single-line and Multi-line comments
Part 2: Program structure

In this part we talk about program structures, such as statements and statement lists, blocks and recursive production rules. In addition we discuss different AST formats and start building more complex expressions.

RDP_Lecture_4_StatementList_cover_small.png
  • TDD: Test-driven development
  • AST Explorer
  • Statements and Statement list
  • Left-recursion
  • Recursion-to-loop conversion to handle left-recursive grammars

Lecture 5: Blocks: nested scopes
RDP_Lecture_5_Blocks_cover_small.png
  • Block statement
  • Nested scopes
  • Empty statement
Lecture 6: Different AST formats
RDP_Lecture_6_AST_formats_cover_small.png
  • AST formats
  • Node handles vs. Node factories
  • Default AST factory
  • S-expression AST factory
  • AST transforms

Lecture 7: Binary Expressions
RDP_Lecture_7_Binary_Expressions_cover_small.png
  • Binary expressions
  • Additive expression
  • Multiplicative expression
  • Primary expression
  • Operator precedence
  • Parenthesized expression
Part 3: Control flow and Functions

In this part we implement variables, assignment, work with operator precedence, and introduce function abstraction. In addition we define control structures such as If-statement and iteration loops.

  • Lecture 8: Assignment Expression
RDP_Lecture_8_AssignmentExpression_cover_small.png
  • Identifiers: variable names
  • Chained assignment
  • Left-hand side expression

Lecture 9: Variable Statement
RDP_Lecture_9_Variables_cover_small.png
  • Variable statement
  • Keyword tokens
  • Variable declarations
  • Name and optional Initializer

Lecture 10: If-Statement
RDP_Lecture_10_If_Statement_cover_small.png
  • Control flow
  • If-else statement
  • Consequent and Alternate parts
  • Relational expression

Lecture 11: Equality | Logical
RDP_Lecture_11_Equality_Expr_cover_small.png
  • Equality expression
  • Logical AND expression
  • Logical OR expression
  • Boolean literals
  • Null literal
Lecture 12: Unary Expression
RDP_Lecture_12_Unary_Expr_cover_small.png
  • Unary expression
  • Logical NOT operator
  • Minus operator
  • Single operand

Lecture 13: Iteration Statement
RDP_Lecture_13_Iteration_Stmp_cover_small.png
  • Control flow
  • Iteration Statement
  • While loop
  • Do-while loop
  • For-cycle
  • Inline variable declaration

Lecture 14: Function Declaration
RDP_Lecture_14_Function_Decl_cover_small.png
  • Function declaration
  • Return statement
  • Formal parameters
  • Function body
  • Optional return
Part 4: Object-oriented programming

The final part of the course we implement classes and objects, talk about property and array access. In addition we implement generic function and method calls, and build the final parser executable.


  • Lecture 15: Member Expression
RDP_Lecture_15_Member_Expr_cover_small.png
  • Member Expression
  • Property access
  • Objects and Properties
  • Computed vs. Non-computed properties
  • Chained objects
  • Assigning to object properties

Lecture 16: Call Expression
RDP_Lecture_16_Call_Expr_cover_small.png
  • Call Expression
  • Function calls
  • Method calls
  • Call | Arguments
  • Chained calls

Lecture 17: OOP | Classes
RDP_Lecture_17_OOP_cover_small.png
  • Object-oriented programming
  • Class declaration
  • New expression
  • Super calls
  • Methods
Lecture 18: Final Executable
RDP_Lecture_18_Final_Exec_cover_small.png
  • Parser CLI
  • Parsing expressions and files
  • Project overview
  • Next steps
  • Further related classes

I hope you’ll enjoy the class, and will be glad to discuss any questions and suggestion in comments.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK