代码改变世界

编译原理Tiny语言的定义

  youxin  阅读(2139)  评论(0编辑  收藏  举报

Here is the definition for Tiny language

The Tiny lexicon is as follows:

  • Keywords:   IF ELSE WRITE READ RETURN BEGIN END MAIN INT REAL
  • Single-character separators:   ;  ,  (   )
  • Single-character operators:    +  -  *   /
  • Multi-character operators:    :=  ==   !=
  • Identifier: An identifier consists of a letter followed by any number of letters or digits. The following are examples of identifiers: x, x2, xx2, x2x, End, END2.Note that End is an identifier while END is a keyword. The following are not identifiers:
    • IF, WRITE, READ, .... (keywords are not counted as identifiers)
    • 2x (identifier can not start with a digit)
    • Strings in comments are not identifiers.
  • Number is a sequence of digits, or a sequence of digits followed by a dot, and followed by digits.   
    Number -> Digits | Digits '.' Digits
    Digits -> Digit | Digit Digits
    Digit  -> '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  • Comments: string between /** and **/. Comments can be longer than one line. 

    The EBNF Grammar

    High-level program structures

    Program -> MethodDecl MethodDecl* 
    MethodDecl -> Type [MAIN] Id '(' FormalParams ')' Block
    FormalParams -> [FormalParam ( ',' FormalParam )* ]
    FormalParam -> Type Id
    
    Type -> INT | REAL
    

    Statements

    Block -> BEGIN Statement* END
    
    Statement -> Block
               | LocalVarDecl  
               | AssignStmt   
               | ReturnStmt
               | IfStmt
    	   | WriteStmt
    	   | ReadStmt
            
    LocalVarDecl -> INT Id ';' | REAL Id ';' 
    
    AssignStmt  -> Id := Expression ';'
    ReturnStmt -> RETURN Expression ';'
    IfStmt    -> IF '(' BoolExpression ')' Statement
                | IF '(' BoolExpression ')' Statement ELSE Statement
    WriteStmt -> WRITE '(' Expression ',' QString ')' ';'
    ReadStmt  -> READ '(' Id ',' QString ')' ';'
    QString is any sequence of characters except double quote itself, enclosed in double quotes.
    

    Expressions

    Expression -> MultiplicativeExpr  (( '+' | '-' ) MultiplicativeExpr)*
    MultiplicativeExpr -> PrimaryExpr (( '*' | '/' ) PrimaryExpr)*
    PrimaryExpr -> Num  // Integer or Real numbers
                 | Id            
                 | '(' Expression ')'
                 | Id '(' ActualParams ')'
    BoolExpression -> Expression '==' Expression 
                     |Expression '!=' Expression   
    ActualParams -> [Expression ( ',' Expression)*]
    
    

    Sample program

     /** this is a comment line in the sample program **/
     INT f2(INT x, INT y ) 
     BEGIN 
        INT z;
        z := x*x - y*y;
        RETURN z; 
     END 
     INT MAIN f1() 
     BEGIN
        INT x;
        READ(x, "A41.input");
        INT y;
        READ(y, "A42.input");
        INT z;
        z := f2(x,y) + f2(y,x);
        WRITE (z, "A4.output"); 
     END

转自:http://cs.uwindsor.ca/~jlu/214/language.htm

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2012-07-19 java.awt.list java.util.list 区别
2012-07-19 网络爬虫 简介
2012-07-19 c++ 容器定义的几种类型
2012-07-19 观察者模式1(observer)
2012-07-19 Java ArrayList 使用
点击右上角即可分享
微信分享提示