GithubHelp home page GithubHelp logo

a-simple-c-interpreter's Introduction

A-Simple-C-Interpreter

Project Description

The main purpose of this project is to implement a simple C interpreter with ANTLR and JAVA.

  • generate a lexical analyzer to recognize the vocabulary of c subset and transform a string of characters into a string of words or tokens
  • generate a parser to verify the strings can be generated by the grammar of C.
  • action with the parser we generate

Run

You have to make the antlr-3.5.2-complete.jar and makefile in current folder and the environment of java has been installed,the run the following command:
(if you want to run test1.c with the simple C interpreter)

make 
java -cp antlr-3.5.2-complete.jar:. myInterp_test test1.c

or

java -cp ./antlr-3.5.2-complete.jar org.antlr.Tool myInterp.g 
javac -cp ./antlr-3.5.2-complete.jar:. *.java
java -cp antlr-3.5.2-complete.jar:. myInterp_test test1.c

the C subset supported by the interpreter

  1. 包含以下關鍵字

    else if int return void
  2. 其中之資料型態 (宣告變數)
    int

  3. 註解方式:

    • /*……*/
      以正斜線和星號組合 (/*) 為開頭的字元序列,可以包含可表示字元集的任何字元組合,但不包括「結尾註解」分隔符號(*/)。
      註解可佔用一行以上,但不可以是巢狀(EX: /*/*……*/ =>error)。
    • //…… 使用兩個正斜線的單行註解 (//)
  4. program statement相關之邏輯、數學運算子

    • 數學運算 : + , - , *, / , %
    • 比較運算 : > , < , == , <= , >= , != , && , ||
  5. 其他標點符號
    ',' 、 '=' 、 ':' 、 ';' 、 '(' 、 ')' 、 '{' 、 '}'

  6. 支援之程式控制結構
    有關條件判斷:If 、 else

  7. 函式
    支援MAIN 函數 PRINTF 函數(可含一個、兩個或三個參數)

  8. 字串及字元
    字串 "……" => 字串會由兩個雙引號包住,且若字串內要使用雙引號,需在其前面加上 \。

  9. 數字
    EX:3、-44、+1

  10. 空白
    遇到 ' '、'\r'、'\t'、'\n' 略過。

The functions the simple c interpreter provides

  1. 使用int main 或 void main開頭都行
  2. 宣告同時也能賦值以及宣告多個
//Example
int a;    
int b = 3;    
int a,b;    
  1. if-then-else 可使用多個接在一起
  2. if-then、if-then-else可巢狀使用
  3. if-then、if-then-else若要做內只有一行,不必加{}
//Example
if(a>1) 
  printf("a>1"); 
  1. scanf 函數(支援輸入一個或兩個)
//Example
scanf("%d %d",&a ,&b); 
scanf("%d ",&a);  
  1. printf 函數(可含一個、兩個或三個參數)
//Example
printf("%d %d" ,a,b);  
printf("%d " ,3);  
printf("ccc");  

%% 、 \n 、 \ 、 \t、 " are same as C language

//Example
printf("\"\\\\a\n\ta\n");

output:

"\\a
    a
  1. if-then、if-then-else能支持較複雜條件判斷
//Example
if ((a==b+1)&&3>1||8<=2)  
  printf("a");
  1. 要return 不 return 都行
  2. 支援註解
  3. ; 或 a+1; ……等,也能為一個statement
//Example
;
a+1;
  1. 在if-then、if-then-else中,有辦法使local variable 有自己的scope,且若此scope沒有會找外面scope的。
//Example
int a =0;
if(1){
  int a =1;
  if(1){ printf(“%d”,a); }
}
printf(“%d”,a); 

output:

10

a-simple-c-interpreter's People

Contributors

gino79445 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.