编译原理 Principles of Compilers
Overview
In this course we mainly learned about the function of compiler and process of compiling.
Compiler is like a translator,it translates source code into assembly language supported by the target machine.
Process of compiling has 6 steps:
In this process, we focused on lexical analysis and syntax analysis.
一、Lexical Analysis
This phase scans the source code and as a stream of characters and recognize different types of tokens, like identifier, keyword or operator.
For example:
int val = 10;
To recognize a token, we need to use something to formalize the description of token.
Like regular expression and finite automata.
1. Regular Expression
A regular expression can represent a type of strings.
We use regular expressions to match the strings we need.
Like:
(a | b)*
This regx represent all strings consists of 'a' and 'b'.
2. Finite Automata
FA is a machine that recognizes regular expressions.
It has a set of states and rules for moving from one state to another.
There are two types of FA:
- DFA: Deterministic Finite Automata
- NFA: Nondeterministic Finite Automata
1) DFA
"Deterministic" means that, for each state, each input symbol corresponds to only one target state.
2) NFA
"Nondeterministic" means that, for each state, each input symbol corresponds to one or more target states.
NFA can be converted to DFA.
二、Syntax Analysis
This phase checks whether the token string given by the lexical analysis conforms to the grammar of source code language.
We use context-free grammar(CFG) to represent a grammar.
1. CFG
A CFG has four components:
-
Non-Terminals(V): It denote sets of strings.
-
Terminal Symbols(Σ): or a set of tokens.
-
Productions(P): The rules. In CFG, left side of productions are non-terminals.
-
Start symbol(S)
Example: rules of arithmetic expression:
2. Syntax Analyzer
Syntax analyzer check the input according to the CFG. Output of this phase is a parse tree.
For example:
input: num1 + num2 * num1
Derivation Process:
The Tree:
3. Two Types of Parsing
1) Top-Down
LL Parser.
2) Bottom-Up
SLR Parser, LR Parser and LALR Parser.
三、课程设计内容
The professor gave us three context-free grammars, and asked us to choose one grammar.
And then write a lexical analyzer, syntax analyzer based on SLR(1) parsing in C++ language.
We could also choose to write a semantic analyzer.
I chose the most difficult grammar and enlisted to write a semantic analyzer. But unfortunately I didn't make it to finish the semantic analyzer.
原文地址:https://www.cnblogs.com/danielwong2021/p/15380761.html
- Python学习笔记1——斐波那契数列
- 视觉传感器几大技术要点详解!
- Spark之搜狗日志查询实战
- 区块链与数字货币是什么关系呢?
- 保存数据到MySql数据库——我用scrapy写爬虫(二)
- 人工智能将取代人类?危机亦或是新的机遇
- 大数据驱动的未来网络:体系架构与应用场景(下)网络架构与场景详解
- 冷静点,NVIDIA 禁止 Geforce 进数据中心想限制的并不是深度学习
- 智能机器人崛起背后的中国力量
- 企业微服务架构转型-实施步骤
- Andrew Ng机器学习课程笔记--week2(多元线性回归&正规公式)
- 科技第六感:黑客控制你的车!不信?其实很简单
- python多版本的pip共存问题解决办法
- C++ 对vector进行排序
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 教你3分钟了解Android 简易时间轴的实现方法
- Android网络技术HttpURLConnection详解
- Ubuntu16.04下CUDA8.0和CUDA9.0共存
- Ubuntu 20.04 开启隐藏录音降噪功能(推荐)
- 解密 Linux 版本信息的方法
- ubuntu20.04连接wifi的方法(2种)
- 服务器Centos部署MySql并连接Navicat过程详解
- Android横竖屏幕切换生命周期详解
- LINUX中如何查看某个端口是否被占用的方法
- Android实现RecyclerView添加分割线的简便方法
- Android定时器Timer的停止和重启实现代码
- Ubuntu20.04开启root账户的方法步骤
- Android 将view 转换为Bitmap出现空指针问题解决办法
- Android ScrollView 下嵌套 ListView 或 GridView出现问题解决办法
- Ubuntu 安装cuda10.1驱动的实现步骤