예제 #1
0
int main()
{
    FILE *pascal_file,*lex_file;
    char file_name[40], file_name2[40];
    char is_token[40];
    char current_char;
    char before_current_char;
    int i=0,j,is_comment=0,is_assignment=0;
    printf("Please enter the source file name: ");
    fflush(stdin);
    gets(file_name);
    strcpy(file_name2,file_name);
    strcat(file_name,".pas");
    strcat(file_name2,".lex");


    for(j=0;j<40;j++)
        is_token[j]=0;
    if((pascal_file = fopen(file_name,"r")) == NULL)
        printf("dosya acilamadi!\n");
    else
    {
        lex_file = fopen(file_name2,"w");
        while(!feof(pascal_file))
        {

            current_char=getc(pascal_file);
            if(isLetter(current_char)==1)//current_char harfse string e eklenir
            {
                is_token[i]=current_char;
                i++;
            }
            else if(isLetter(current_char)==0 && i!=0)// current char harf degilse ondan onceki stringi(is_letter) yazdýr
            {
                if(strcmp("writeln",is_token)==0 || strcmp("write",is_token)==0)
                {
                    fputs("output(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                else if(strcmp("readln",is_token)==0 || strcmp("read",is_token)==0)
                {
                    fputs("input(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                else if(isReservedWord(is_token)==1)
                {
                    fputs("reservedWord(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                else if(isAdvMathFunc(is_token)==1)
                {
                    fputs("advancedMathFunction(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                else if(isOrdinalFunc(is_token)==1)
                {
                    fputs("ordinalFunction(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                else if(isVariableType(is_token)==1)
                {
                    fputs("variableType(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                else if(isBoolOperator(is_token)==1)
                {
                    fputs("booleanOperator(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                else if(isFileHandlingFunc(is_token)==1)
                {
                    fputs("fileHandlingFunction(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                else if(strcmp(is_token,"div")==0 || strcmp(is_token,"mod")==0)
                {
                    fputs("arithmeticOperation(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                else
                {
                    fputs("variable(",lex_file);
                    fputs(is_token,lex_file);
                    fputs("),",lex_file);
                }
                i=0;
                for(j=0;j<40;j++)
                    is_token[j]=0;
            }

            if(current_char==':')
            {
                current_char=getc(pascal_file);
                if(current_char=='=')
                {
                    fputs("assignmentOperator(:=),",lex_file);
                    is_assignment=1;
                }
                else
                {
                    fputs("colon(:),",lex_file);
                    ungetc(current_char,pascal_file);
                }
            }


            if(current_char=='{')
            {
                comment(pascal_file,current_char,lex_file);
            }


            if(current_char=='*' && is_comment==1)
            {
                comment(pascal_file,current_char,lex_file);
                is_comment=0;
            }
            if(is_comment==1)
            {
                fputs("leftParentheses((),",lex_file);
            }
            if(current_char==')')
            {
                fputs("rightParentheses()),",lex_file);
            }
            if(current_char==';')
            {
                fputs("endOfLine(;),",lex_file);
            }
            if(current_char=='[')
            {
                fputs("openingBracket([)",lex_file);
            }
            if(current_char==']')
            {
                fputs("closingBracket(])",lex_file);
            }



           if(is_assignment==0 && (current_char=='<' || current_char=='>' || current_char=='='))
            {
                before_current_char=current_char;
                current_char=getc(pascal_file);
                if(before_current_char=='<' && (current_char=='=' || current_char=='>'))
                {
                    fputs("compOperator(",lex_file);
                    fputc(before_current_char,lex_file);
                    fputc(current_char,lex_file);
                    fputs("),",lex_file);
                }
                else if(before_current_char=='>' && current_char=='=')
                {
                    fputs("compOperator(",lex_file);
                    fputc(before_current_char,lex_file);
                    fputc(current_char,lex_file);
                    fputs("),",lex_file);
                }
                else if(before_current_char=='<' || before_current_char=='>' || before_current_char=='=')
                {
                    fputs("compOperator(",lex_file);
                    fputc(before_current_char,lex_file);
                    fputs("),",lex_file);
                    ungetc(current_char,pascal_file);
                }

            }
            is_assignment=0;




            if(current_char=='+' || current_char =='-' || current_char=='*' || current_char=='/')
            {
                fputs("arithOperator(",lex_file);
                fputc(current_char,lex_file);
                fputs("),",lex_file);
            }

            is_comment=0;
            if(current_char=='(')
            {
                is_comment=1;
            }
            if(current_char==39)
            {
                skip_string(pascal_file,lex_file);
            }

            if(current_char==9 || current_char==10 || current_char==' ')
            {
                fputc(current_char,lex_file);
            }
            /*if(current_char==';')
            {

                i=0;
                for(j=0;j<40;j++)
                    is_token[j]='\0';
            }*/
            if(isNumeric(current_char)==1)
            {
                constant(pascal_file,lex_file,current_char);
            }

        }
        fclose(lex_file);
        lex_file = fopen(file_name2,"r");
        current_char=getc(lex_file);
        while(!feof(lex_file))
        {
            printf("%c",current_char);
            current_char=getc(lex_file);
        }
    }

    return 0;
}
	const glu::VarType*				getVariableType			(void) const { DE_ASSERT(isVariableType()); return m_data.type;			}