[关闭]
@weixin 2015-04-16T16:34:40.000000Z 字数 4278 阅读 1188

compiler study 2 - crowbar

compile


lex

  1. <INITIAL>"function" return FUNCTION;
  2. <INITIAL>"if" return IF;
  3. <INITIAL>"else" return ELSE;
  4. ...
  5. <INITIAL>"-" return SUB;
  6. <INITIAL>"*" return MUL;
  7. <INITIAL>"/" return DIV;
  8. <INITIAL>"%" return MOD;
  9. <INITIAL>[A-Za-z_][A-Za-z_0-9]* {
  10. yylval.identifier = crb_create_identifier(yytext);
  11. return IDENTIFIER;
  12. }
  1. <INITIAL>([1-9][0-9]*)|"0" {
  2. Expression *expression = crb_alloc_expression(INT_EXPRESSION);
  3. sscanf(yytext, "%d", &expression->u.int_value);
  4. yylval.expression = expression;
  5. return INT_LITERAL;
  6. }
  1. <INITIAL>\" {
  2. crb_open_string_literal();
  3. BEGIN STRING_LITERAL_STATE;
  4. }
  1. <STRING_LITERAL_STATE>\" {
  2. Expression *expression = crb_alloc_expression(STRING_EXPRESSION);
  3. expression->u.string_value = crb_close_string_literal();
  4. yylval.expression = expression;
  5. BEGIN INITIAL;
  6. return STRING_LITERAL;
  7. }
  8. <STRING_LITERAL_STATE>\n {
  9. crb_add_string_literal('\n');
  10. increment_line_number();
  11. }
  12. <STRING_LITERAL_STATE>\\\" crb_add_string_literal('"');
  13. <STRING_LITERAL_STATE>\\n crb_add_string_literal('\n');
  14. <STRING_LITERAL_STATE>\\t crb_add_string_literal('\t');
  15. <STRING_LITERAL_STATE>\\\\ crb_add_string_literal('\\');
  16. <STRING_LITERAL_STATE>. crb_add_string_literal(yytext[0]);

when it is in STRING_LITERAL_STATE, if see another ", then the string is closed, the allocate a string expression in interpreter. then BEGIN INITIAL, return to the initial status.

  1. <INITIAL>. {
  2. char buf[LINE_BUF_SIZE];
  3. if (isprint(yytext[0])) {
  4. buf[0] = yytext[0];
  5. buf[1] = '\0';
  6. } else {
  7. sprintf(buf, "0x%02x", (unsigned char)yytext[0]);
  8. }
  9. crb_compile_error(CHARACTER_INVALID_ERR,
  10. STRING_MESSAGE_ARGUMENT, "bad_char", buf,
  11. MESSAGE_ARGUMENT_END);
  12. }

. stands for any character other than the pre-defined <INITIAL> isprint is a c standard lib function, check if the char printable or not, then report compiler error.

yacc

define function

  1. function_definition
  2. : FUNCTION IDENTIFIER LP parameter_list RP block
  3. {
  4. crb_function_define($2, $4, $6);
  5. }
  6. | FUNCTION IDENTIFIER LP RP block
  7. {
  8. crb_function_define($2, NULL, $5);
  9. }
  10. ;

in crowbar, the function syntax like this :
function func_name(param1, param2..){} or function func_name(){}

Created with Raphaël 2.1.2Startcrb_search_functionyes or nocrb_get_current_interpretercrb_malloc(FuctionDefinition)inter->function_list=fEndyesno

define parameter

  1. parameter_list
  2. : IDENTIFIER
  3. {
  4. $$ = crb_create_parameter($1);
  5. }
  6. | parameter_list COMMA IDENTIFIER
  7. {
  8. $$ = crb_chain_parameter($1, $3);
  9. }
  10. ;
  11. argument_list
  12. : expression
  13. {
  14. $$ = crb_create_argument_list($1);
  15. }
  16. | argument_list COMMA expression
  17. {
  18. $$ = crb_chain_argument_list($1, $3);
  19. }
  20. ;

what's the differences between parmeter and argument? here is a link : parameter vs argument
function A(p1,p2), p1, p2 are parameters, which are part of method signature.
b = A(arg1,arg2), arg1 and arg2 are arguments, which passed in when you call the method.
think parameter is a parking lot, or placeholder, argument is a car.

define expression

  1. expression
  2. : logical_or_expression
  3. | IDENTIFIER ASSIGN expression
  4. {
  5. $$ = crb_create_assign_expression($1, $3);
  6. }
  7. ;
  8. logical_or_expression
  9. : logical_and_expression
  10. | logical_or_expression LOGICAL_OR logical_and_expression
  11. {
  12. $$ = crb_create_binary_expression(LOGICAL_OR_EXPRESSION, $1, $3);
  13. }
  14. ;
Created with Raphaël 2.1.2Startcrb_create_binary_expressioncheck expression typeint,double or nocrb_eval_binary_expressionconvert_value_to_expressionEndcrb_alloc_expressioninsert left and right childyesno

after checking expresison type, if the type is INT_EXPRESSION or DOUBLE_EXPRESSION, then it will crb_eval_binary_expression, this is called variable folding.

define statement

  1. statement
  2. : expression SEMICOLON
  3. {
  4. $$ = crb_create_expression_statement($1);
  5. }
  6. | global_statement
  7. | if_statement
  8. | while_statement
  9. | for_statement
  10. | return_statement
  11. | break_statement
  12. | continue_statement
  13. ;
  14. global_statement
  15. : GLOBAL_T identifier_list SEMICOLON
  16. {
  17. $$ = crb_create_global_statement($2);
  18. }
  19. ;
  20. ....
  21. if_statement
  22. : IF LP expression RP block
  23. {
  24. $$ = crb_create_if_statement($3, $5, NULL, NULL);
  25. }
  26. | IF LP expression RP block ELSE block
  27. {
  28. $$ = crb_create_if_statement($3, $5, NULL, $7);
  29. }
  30. | IF LP expression RP block elsif_list
  31. {
  32. $$ = crb_create_if_statement($3, $5, $6, NULL);
  33. }
  34. | IF LP expression RP block elsif_list ELSE block
  35. {
  36. $$ = crb_create_if_statement($3, $5, $6, $8);
  37. }
  38. ;
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注