Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_TEXT_LEN 1000
- enum{
- ID,
- VAL_INT,
- VAL_REAL,
- VAL_STR,
- VAR,FUNCTION,IF,ELSE,WHILE,END,RETURN,INT,REAL,STR,COLON,SEMICOLON,
- LPAR,RPAR,COMMA,OR,AND,NOT,EQUAL,NOTEQUAL,LESS,ASSIGN,ADD,SUB,MUL,DIV,FINISH};
- typedef struct{
- int tipat;
- union{
- char text[MAX_TEXT_LEN];
- int nrInt;
- double nrReal;
- };
- }Atom;
- int nAtoms=0;
- Atom atom[1000];
- FILE *fis;
- void err(char* e){
- printf("Eroare: %s ",e);
- exit(0);
- }
- void adaugaAtom(int tip){
- atom[nAtoms].tipat=tip;
- nAtoms++;
- }
- int lexer()
- {
- char buf[MAX_TEXT_LEN];
- int n=0;
- int state=0;
- int ch;
- int nrInt;
- double nrReal;
- for(;;)
- {
- if(state!=2 && state!=4 && state!=7 && state!=9 && state!=10 && state!=11 && state!=12 && state!=13 && state!=14 && state!=16 && state!=18 && state!=20 && state!=21 && state!=23 && state!=24 && state!=25 && state!=26 && state!=27 && state!=28 && state!=29 && state!=30)
- {
- ch=fgetc(fis);
- }
- // printf("# %d,%c(%d)\n",state,ch,ch);
- switch(state)
- {
- case 0:
- if(isalpha(ch)||ch=='_')
- {
- state=1;
- buf[n++]=ch;
- }
- else if(isdigit(ch))
- {
- state=3;
- buf[n++]=ch;
- }
- else if(ch=='"')
- {
- state=8;
- }
- else if(ch==':')
- {
- state=10;
- }
- else if(ch==';')
- {
- state=11;
- }
- else if(ch=='(')
- {
- state=12;
- }
- else if(ch==')')
- {
- state=13;
- }
- else if(ch==',')
- {
- state=14;
- }
- else if(ch=='|')
- {
- state=15;
- }
- else if(ch=='&')
- {
- state=17;
- }
- else if(ch=='!')
- {
- state=19;
- }
- else if(ch=='=')
- {
- state=22;
- }
- else if(ch=='<')
- {
- state=25;
- }
- else if(ch=='+')
- {
- state=26;
- }
- else if(ch=='-'){
- state=27;
- }
- else if(ch=='*')
- {
- state=28;
- }
- else if(ch=='/')
- {
- state=29;
- }
- else if(ch==' '|| ch=='\n'|| ch=='\t'||ch=='\r')
- {
- state=0;
- }
- else if(ch==EOF)
- {
- state=30;
- return FINISH;
- }
- else
- {
- err("Caracter necunoscut!");
- }
- break;
- case 1:
- if(isalnum(ch)||ch=='_')
- {
- buf[n++]=ch;
- }
- else
- {
- ungetc(ch,fis);
- state=2;
- }
- break;
- case 2:
- buf[n]='\0';
- if(!strcmp(buf,"var")){
- adaugaAtom(VAR);
- return VAR;
- }
- else if(!strcmp(buf,"function")){
- adaugaAtom(FUNCTION);
- return FUNCTION;
- }
- else if(!strcmp(buf,"if")){
- adaugaAtom(IF);
- return IF;
- }
- else if(!strcmp(buf,"else")){
- adaugaAtom(ELSE);
- return ELSE;
- }
- else if(!strcmp(buf,"while")){
- adaugaAtom(WHILE);
- return WHILE;
- }
- else if(!strcmp(buf,"end")){
- adaugaAtom(END);
- return END;
- }
- else if(!strcmp(buf,"return")){
- adaugaAtom(RETURN);
- return RETURN;
- }
- else if(!strcmp(buf,"int")){
- adaugaAtom(INT);
- return INT;
- }
- else if(!strcmp(buf,"real")){
- adaugaAtom(REAL);
- return REAL;
- }
- else if(!strcmp(buf,"str")){
- adaugaAtom(STR);
- return STR;
- }
- else{
- //daca nu e cuvant cheie,este ID
- adaugaAtom(ID);
- strcpy(atom[nAtoms-1].text,buf);
- return ID;
- }
- case 3:
- if(isdigit(ch)) {
- buf[n++]=ch;
- }
- else if( ch=='.')
- {
- state=5;
- buf[n++]=ch;
- }
- else
- {
- ungetc(ch,fis);
- state=4;
- }
- break;
- case 4:
- buf[n]='\0';
- adaugaAtom(VAL_INT);
- atom[nAtoms-1].nrInt=atoi(buf);
- return VAL_INT;
- break;
- case 5:
- if(isdigit(ch)){
- state=6;
- buf[n++]=ch;
- }
- else{
- err("Lipsesc zecimalele(cifrele de dupa punct)");
- }
- break;
- case 6:
- if(isdigit(ch)){
- buf[n++]=ch;
- }
- else{
- ungetc(ch,fis);
- state=7;
- }
- break;
- case 7:
- buf[n]='\0';
- adaugaAtom(VAL_REAL);
- atom[nAtoms-1].nrReal=atof(buf);
- return VAL_REAL;
- break;
- case 8:
- if(ch!='"'){
- buf[n++]=ch;
- }
- else{
- state=9;
- }
- break;
- case 9:
- buf[n]='\0';
- adaugaAtom(VAL_STR);
- strcpy(atom[nAtoms-1].text,buf);
- return VAL_STR;
- break;
- case 10:
- adaugaAtom(COLON);
- return COLON;
- break;
- case 11:
- adaugaAtom(SEMICOLON);
- return SEMICOLON;
- break;
- case 12:
- adaugaAtom(LPAR);
- return LPAR;
- break;
- case 13:
- adaugaAtom(RPAR);
- return RPAR;
- break;
- case 14:
- adaugaAtom(COMMA);
- return COMMA;
- break;
- case 15:
- if(ch=='|'){
- state=16;
- buf[n++]=ch;
- }
- break;
- case 16:
- buf[n]='\0';
- adaugaAtom(OR);
- strcpy(atom[nAtoms-1].text,buf);
- return OR;
- break;
- case 17:
- if(ch=='&'){
- state=18;
- buf[n++]=ch;
- }
- break;
- case 18:
- buf[n]='\0';
- adaugaAtom(AND);
- strcpy(atom[nAtoms-1].text,buf);
- return AND;
- break;
- case 19:
- if(ch=='='){
- state=21;
- //buf[n++]=ch;
- }
- else{
- ungetc(ch,fis);
- adaugaAtom(NOT);
- return NOT;
- }
- break;
- case 21:
- buf[n]='\0';
- adaugaAtom(NOTEQUAL);
- strcpy(atom[nAtoms-1].text,buf);
- return NOTEQUAL;
- break;
- case 22:
- buf[n]='\0';
- state=24;
- if(ch=='='){
- state=23;
- buf[n++]=ch;
- }
- break;
- case 24:
- buf[n]='\0';
- adaugaAtom(ASSIGN);
- return ASSIGN;
- break;
- case 23:
- buf[n]='\0';
- adaugaAtom(EQUAL);
- strcpy(atom[nAtoms-1].text,buf);
- return EQUAL;
- break;
- case 25:
- adaugaAtom(LESS);
- return LESS;
- break;
- case 26:
- adaugaAtom(ADD);
- return ADD;
- break;
- case 27:
- adaugaAtom(SUB);
- return SUB;
- break;
- case 28:
- adaugaAtom(MUL);
- return MUL;
- break;
- case 29:
- adaugaAtom(DIV);
- return DIV;
- break;
- case 30:
- adaugaAtom(FINISH);
- return FINISH;
- break;
- default:
- printf("Stare neimplementata: %d", state);
- }
- }
- }
- char* afis_nume(int n){
- switch(n){
- case 0:
- return "ID";
- case 1:
- return "VAL_INT";
- case 2:
- return "VAL_REAL";
- case 3:
- return "VAL_STR";
- case 4:
- return "VAR";
- case 5:
- return "FUNCTION";
- case 6:
- return "IF";
- case 7:
- return "ELSE";
- case 8:
- return "WHILE";
- case 9:
- return "END";
- case 10:
- return "RETURN";
- case 11:
- return "INT";
- case 12:
- return "REAL";
- case 13:
- return "STR";
- case 14:
- return "COLON";
- case 15:
- return "SEMICOLON";
- case 16:
- return "LPAR";
- case 17:
- return "RPAR";
- case 18:
- return "COMMA";
- case 19:
- return "OR";
- case 20:
- return "AND";
- case 21:
- return "NOT";
- case 22:
- return "EQUAL";
- case 23:
- return "NOTEQUAL";
- case 24:
- return "LESS";
- case 25:
- return "ASSIGN";
- case 26:
- return "ADD";
- case 27:
- return "SUB";
- case 28:
- return "MUL";
- case 29:
- return "DIV";
- case 30:
- return "FINISH";
- default: return "Eroare !";
- }
- }
- int main()
- {
- if((fis=fopen("program.q","r"))==NULL)
- {
- printf("Eroare la deschidere!");
- return -1;
- }
- while(lexer()!=FINISH) {
- // afisare atomi
- if(atom[nAtoms-1].tipat==1){
- printf("%s %d \n",afis_nume(atom[nAtoms-1].tipat),atom[nAtoms-1].nrInt);
- }
- else if(atom[nAtoms-1].tipat==2){
- printf("%s %f \n",afis_nume(atom[nAtoms-1].tipat),atom[nAtoms-1].nrReal);
- }
- else if(atom[nAtoms-1].tipat==3){
- printf("%s %s\n",afis_nume(atom[nAtoms-1].tipat), atom[nAtoms-1].text);
- }
- else
- printf("%s %s\n",afis_nume(atom[nAtoms-1].tipat), atom[nAtoms-1].text);
- }
- printf("FINISH");
- fclose(fis);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement