Coder Social home page Coder Social logo

c0-compiler's Introduction

c-compiler

c compiler with lex and yacc

q1

q2

Note: There are 1 shift/reduce conflicts, correctly resolved by default: IF '(' expression ')' statement _ ELSE statement

Solve unary via %prec

Solve ++ with lexer INC_OP

union define tokens, pass yylval.str from lex to yacc

%option yylineno use linenon

yacc recursive $$ $1

TODO

lineno

c0 tokens

public enum TokenType 
{
	//关键字
	IF,ELSE,WHILE,RETURN,VOID,INT, 
	//运算符 + - * /  =  <  <=  >  >=  !=  … 
	PLUS,MINUS,STAR,SLASH, LT,LTEQ,GT,GTEQ,EQ,NEQ,ASSIGN,
	//界符 ;  ,  (  )  [  ]  {  }  /*  */
	SEMI,COMMA,LPAREN,RPAREN,LSQUAR,RSQUAR,LBRACE,RBRACE,
	LCOMMENT,RCOMMENT,
	ID, 				//标识符
	NUMBER, 			//数字常量

『 ID→letter(letter|didit)*           NUMBER→digit digit *                letter→a|b|…|z|A|B|…|Z                digit→0|…|9 』 NONTOKEN,ERROR,ENDFILE // 其它 };

c0 pattern

program→ { var-declaration | fun-declaration } var-declaration→ int ID { , ID }
fun-declaration→ ( int | void ) ID ( params ) compound-stmt params → int ID { , int ID } | void | empty compound-stmt→ { { var-declaration } { statement } } statement→ expression-stmt∣compound-stmt ∣if-stmt ∣while-stmt |return-stmt expression-stmt→ [ expression ] ; if-stmt→ if( expression ) statement [ else statement ] while-stmt→ while( expression ) statement return-stmt→ return [ expression ] ; expression→ ID = expression | simple-expression simple-expression→ additive-expression [ relop additive-expression ] relop → < | <= | > | >= | == | != additive-expression→ term [( + | - ) term ] term→ factor [ ( * | / ) factor ] factor→ ( expression )| ID | call | NUM call→ ID( args ) args→ expression { , expression } | empty ID →… ;参见C语言标识符定义 NUM →… ;参见C语言数的定义

c0-compiler's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.