首页 > 其他分享 >Cornell cs3110 - Chapter9 Lessons

Cornell cs3110 - Chapter9 Lessons

时间:2024-10-08 21:59:42浏览次数:1  
标签:Lessons Int Chapter9 Cornell expr bop token e1 e2

使用 Menhir 构建 SimPL 的编译器

Lexer and Parser 语法分析模块

Lexer, Parser, AST 是三个依次耦合的模块,可以这么描述三者的关系:

Lexer ---tokens--> Parser ---nodes--> AST

相对于上面的图像化描述,cs3110 反过来构建整个 Lexer 和 Parser 的结构

ast.ml 中,定义了 AST 上节点 node 的类型:

type bop = 
  | Add
  | Mul 
  | Leq 

type expr = 
  | Var of string
  | Int of int 
  | Bool of bool
  | Binop of bop * expr * expr 
  | If of expr * expr * expr 
  | Let of string * expr * expr

parser.mly 中构建的 parser 接受 TOKEN 并将其翻译成 AST 上节点

(* Link the ast and the parser *)
%{
    open Ast
%}

(* The name of tokens *)

%token <int> INT
%token <string> ID
%token TRUE
%token FALSE
%token LEQ
%token TIMES
%token PLUS
%token LPAREN
%token RPAREN
%token LET
%token EQUALS
%token IN
%token IF
%token THEN
%token ELSE
%token EOF

(* Compute the precedence and associativity of the operators *)
%nonassoc IN
%nonassoc ELSE
%left LEQ
%left PLUS
%left TIMES

(* Define the start symbol: where to parse the program? *)
%start <Ast.expr> prog

%%

prog:
    | e = expr EOF { e }
    ;

expr: 
    | i = INT { Int i } (* integer *)
    | TRUE { Bool true } (* booleans *)
    | FALSE { Bool false }
    | e1 = expr Add e2 = expr { Binop (Add, e1, e2) } (* binary operators *)
    | e1 = expr Mul e2 = expr { Binop (Mul, e1, e2) }
    | e1 = expr Leq e2 = expr { Binop (Leq, e1, e2) }
    | LET; x = ID; EQUALS; e1 = expr; IN; e2 = expr { Let (x, e1, e2) } (* let expressions *)
    | IF; e1 = expr; THEN; e2 = expr; ELSE; e3 = expr { If (e1, e2, e3) } (* if expressions *)
    | LPAREN; e = expr; RPAREN { e } (* parentheses *)
    ;

lexer.mll 接受输入的 token 序列(也即从字符串角度看代码)并将其进行划分,返回 parser.mly 中早就定义好的 TOKEN

{
    open Parser
}

(* Regular expressions for the lexer *)
let white = [' ' '\t']+
let digit = ['0'-'9']
let int = '-'?digit+
let letter = ['a'-'z' 'A'-'Z']
let id = letter (letter|digit)*

(* Define the rules of lexer *)
rule read = 
    parse 
    (* 'read lexbuf' reads the next token from lexbuf *)
    | white { read lexbuf }
    | "true" { TRUE }
    | "false" { FALSE }
    | "<=" { LEQ }
    | "*" { TIMES }
    | "+" { PLUS }
    | "(" { LPAREN }
    | ")" { RPAREN }
    | "let" { LET }
    | "=" { EQUALS }
    | "in" { IN }
    | "if" { IF }
    | "then" { THEN }
    | "else" { ELSE }
    (* 'Lexing.lexeme lexbuf' returns the string that was matched *)
    | id { ID (Lexing.lexeme lexbuf) }
    (* 'int_of_string' converts a string to an integer *)
    | int { INT (int_of_string (Lexing.lexeme lexbuf)) }
    | eof { EOF }

对于 Menhir 工具来说,这样的构建顺序是合理的;毕竟看上去 lexer.mll 中定义 TOKEN 并不是那么方便

标签:Lessons,Int,Chapter9,Cornell,expr,bop,token,e1,e2
From: https://www.cnblogs.com/sysss-blogs/p/18453125

相关文章

  • Cornell cs3110 - Chapter7 Exercises
    (*Exercise:mutablefields*)typestudent={name:string;mutablegpa:float;}letstuA={name="Alice";gpa=3.7}let()=stuA.gpa<-4.0(*Exercise:intfun*)letinc=ref(funx->x+1)letnum=!inc3109(*Exercise:a......
  • Cornell cs3110 - Chapter6 Exercises
    (*Exercise:specgame*)(*Whereisanotherprogrammer?*)(*Exercise:polyspec*)(*[Poly]representsimmutablepolynomialswithintegercoeffcients*)moduletypePoly=sig(*[t]isthetypeofpolynomials*)typet(*[evalxp]is[p]e......
  • Cornell cs3110 - Chapter5 Exercises
    (*Exercise:complexsynonym*)moduletypeComplexSig=sigtypecomplexvalzero:complexvaladd:complex->complex->complexend(*Exercise:complexencapsulation*)moduleComplex:ComplexSig=structtypecomplex=float*flo......
  • Cornell cs3110 - Chapter4 Exercises
    (*Exercise:mysteryoperator1*)let($)fx=fx;;(*使得函数的连续调用具有一部分右结合的特质square$2+2与square2+2的运行结果分别是16和6*)(*Exercise:repeat*)letrecrepeatfnx=matchnwith|0->x|_->repeatf(n-1)......
  • Cornell cs3110 - Chapter3 Exercises
    (*Exercise:listexpressions*)letlist1=[1;2;3;4;5];;letlist2=1::2::3::4::5::[];;letlist3=[1]@[2;3;4;]@[5];;(*Exercise:product*)letrecproductl=matchlwith|[]->1|h::t->h*productt;;(*......
  • Cornell University's Textbook Reading Systems(教科书阅读系统-康奈尔大学)
    TextbookReadingSystemsTextbookreadingsystemshelpyouinteractwiththeinformationintextbookssothatyoucanbetterinternalizeandlearnSQ3RTheSQ3Risasystematicmethoddesignedforstudyingatextbook.DevelopedbyFrancisP.Robinson,......
  • DCN V2 Improved Deep & Cross Network and Practical Lessons for Web-scale Learnin
    目录概DCN-v2WangR.,ShivannaR.,ChengD.Z.,JainS.,LinD.,HongL.andChiE.D.DCNV2:Improveddeep&crossnetworkandpracticallessonsforweb-scalelearningtoranksystems,2020.概DCN的升级版.DCN-v2DCN-v2的cross/deep的结合方式上有上......
  • Effective-Java-Chapter9-通用程序设计
    https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/dev/Chapter-9/Chapter-9-Introduction.md准则一将局部变量的作用域最小化不要在变量使用之前就申明,在需要使用的时候进行申明。当然这条准则不是那么绝对,大部分时候遵守就好。......
  • chapter9------中断
    中断是什么中断就是打断处理器当前的执行流程,去执行另外一些和当前工作不相干的指令,执行完之后,还可以返回到原来的程序流程继续执行为什么会有中断机制中断这种机制能够让处理器可以在不同任务之间快速切换,实现多任务处理的功能。试想一下没有中断机制,一次只能执行一个任务,那我......
  • 论文阅读:T-RAG: LESSONS FROM THE LLM TRENCHES
    T-RAG:LESSONSFROMTHELLMTRENCHES(https://arxiv.org/abs/2402.07483)https://github.com/jiangnanboy/paper_read_note一.概述大型语言模型(llm)越来越多地应用于各个领域,包括对私有企业文档的问答,其中数据安全性和鲁棒性至关重要。检索增强生成(retrieve-augmented......