Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #####################################
- | lexer start | //made with flex(GNU)
- #####################################
- %{
- #include "y.tab.h"
- %}
- %%
- [ \t\n] {
- /* игнорирование нескольких пробелов подряд, новых строк */
- }
- "$"[a-zA-Z_][a-zA-Z0-9_]* {
- return IDENTIFIER;
- }
- \"[^"]*\" {
- return STRING_LITERAL;
- }
- [0-9]+ {
- return INTEGER;
- }
- [0-9]+(\.)[0-9]+ {
- return FLOAT;
- }
- [><]|">="|"<="|"=="|"!=" {
- return COMPARE_OP;
- }
- "=" {
- return ASSIGN_OP;
- }
- [-+*/] {
- return ARITH_OPP;
- }
- "++"|"--" {
- return INC_OP;
- }
- "(" {
- return L_PAREN;
- }
- ")" {
- return R_PAREN;
- }
- "{" {
- return L_FIG;
- }
- "}" {
- return R_FIG;
- }
- ";" {
- return 0;
- }
- ":" {
- return COLON;
- }
- "if" {
- return IF;
- }
- "else" {
- return ELSE;
- }
- "while" {
- return WHILE;
- }
- "for" {
- return FOR;
- }
- . {
- yyerror("invalid character");
- }
- %%
- int yywrap() {
- return 1;
- }
- #####################################
- | parser start | //made with bison(GNU)
- #####################################
- %{
- #include <stdio.h>
- extern int yylineno;
- extern char *yytext;
- void yyerror(char *);
- %}
- %token IDENTIFIER
- /* statement tokens */
- %token IF
- %token ELSE
- %token WHILE
- %token FOR
- /* bracket tokens */
- %token L_PAREN
- %token R_PAREN
- %token L_FIG
- %token R_FIG
- /* tokens */
- %token COLON
- /* operation tokens */
- %token COMPARE_OP
- %token ASSIGN_OP
- %token ARITH_OPP
- %token INC_OP
- /* type tokens */
- %token INTEGER
- %token FLOAT
- %token STRING_LITERAL
- %%
- block:
- | statement
- | L_FIG block R_FIG
- ;
- statement: assign_statement
- | expression
- | if_statement
- | if_else_statement
- | for_loop
- | while_loop
- ;
- expression: arith_expression
- | logical_expression
- ;
- logical_expression: expression COMPARE_OP expression
- ;
- atom: INTEGER
- | FLOAT
- | STRING_LITERAL
- | IDENTIFIER
- ;
- arith_expression: atom
- | atom ARITH_OPP arith_expression
- ;
- /* STATEMENTS START */
- assign_statement: IDENTIFIER ASSIGN_OP expression { printf("\t\tcorrect ASSIGNMENT statement \n"); }
- ;
- if_statement: IF L_PAREN logical_expression R_PAREN block { printf("\t\tcorrect IF statement \n"); }
- ;
- if_else_statement: IF L_PAREN logical_expression R_PAREN block ELSE block { printf("\t\tcorrect IF-ELSE statement statement \n"); }
- ;
- for_loop: FOR L_PAREN arith_expression logical_expression arith_expression R_PAREN block { printf("\t\tcorrect FOR statement \n"); }
- ;
- while_loop: WHILE L_PAREN logical_expression R_PAREN COLON block { printf("\t\tcorrect WHILE statement \n"); }
- ;
- /* STATEMENTS END */
- %%
- int main() {
- fprintf(stdout, "\n\tThis program receives a text that contains PHP code and checks it\'s correctness\n\n");
- yylineno = 1;
- yyparse();
- fprintf(stdout, "\n\tEnded successfully!\n");
- return 0;
- }
- void yyerror(char *errmsg)
- {
- fprintf(stderr, "\n%s (%d): %s\n", errmsg, yylineno, yytext);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement