C-- 語言

簡介

C— 語言是一個簡化的 C 語言版本,主要是教學用的示範語言,該語言的語法設計簡單而清楚,很適合學習編譯器的同學閱讀。

語法

  1. C-- Language Specification
prog     :    { dcl ';'  |  func }
dcl     :    type var_decl { ',' var_decl }
     |    [ extern ] type id '(' parm_types ')' { ',' id '(' parm_types ')' }
     |    [ extern ] void id '(' parm_types ')' { ',' id '(' parm_types ')' }
var_decl     :    id [ '[' intcon ']' ]
type     :    char
     |    int
parm_types     :    void
     |    type id [ '[' ']' ] { ',' type id [ '[' ']' ] }
func     :    type id '(' parm_types ')' '{' { type var_decl { ',' var_decl } ';' } { stmt } '}'
     |    void id '(' parm_types ')' '{' { type var_decl { ',' var_decl } ';' } { stmt } '}'
stmt     :    if '(' expr ')' stmt [ else stmt ]
     |    while '(' expr ')' stmt
     |    for '(' [ assg ] ';' [ expr ] ';' [ assg ] ')' stmt
     |    return [ expr ] ';'
     |    assg ';'
     |    id '(' [expr { ',' expr } ] ')' ';'
     |    '{' { stmt } '}'
     |    ';'
assg     :    id [ '[' expr ']' ] = expr
expr     :    '–' expr
     |    '!' expr
     |    expr binop expr
     |    expr relop expr
     |    expr logical_op expr
     |    id [ '(' [expr { ',' expr } ] ')' | '[' expr ']' ]
     |    '(' expr ')'
     |    intcon
     |    charcon
     |    stringcon
binop     :    +
     |    –
     |    *
     |    /
relop    :    ==
     |    !=
     |    <=
     |    <
     |    >=
     |    >
logical_op     :    &&
     |    ||
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License