Sorry for a long long delay
As promised I did some experiment but due some other involvement I could not post. Sorry again...
But now i can share some code which translate very few keywords from BASIC to C. Why I choose these 2 language is ease of understanding.
BASIC is like normal English. Even a newcomer can understand the code. C is more professional and has a compiler to compile. BASIC is an interpretive language, Most implementation are interpreter, though some BASIC compilers are available.
Bellow is the code sofar :)
compile under Linux using gcc.
Use a small BASIC file like
REM remarks
FOR i = 0 TO 10
PRINT "Hello World"
NEXT i
a = 5234.9876
b = 123.789
IF a>b THEN b=a
IF a>=b THEN c = a+b
IF a<>b THEN c = b
IF a<=b THEN c = a
IF a<b THEN c =b
PRINT a,b,c
END
and see the generated file and enjoy.
As promised I did some experiment but due some other involvement I could not post. Sorry again...
But now i can share some code which translate very few keywords from BASIC to C. Why I choose these 2 language is ease of understanding.
BASIC is like normal English. Even a newcomer can understand the code. C is more professional and has a compiler to compile. BASIC is an interpretive language, Most implementation are interpreter, though some BASIC compilers are available.
Bellow is the code sofar :)
compile under Linux using gcc.
Use a small BASIC file like
REM remarks
FOR i = 0 TO 10
PRINT "Hello World"
NEXT i
a = 5234.9876
b = 123.789
IF a>b THEN b=a
IF a>=b THEN c = a+b
IF a<>b THEN c = b
IF a<=b THEN c = a
IF a<b THEN c =b
PRINT a,b,c
END
and see the generated file and enjoy.
/*
* Parser for BASIC like language
*
* Copyright 2014 Ashok Shankar Das
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include
#include
#include
#include
//#include "symtab.c"
FILE *source=NULL, *target=NULL, *subf=NULL;
/* ============= Boolean Defination =============== */
typedef enum { false=0,true} bool;
/* ==============Token types ====================== */
typedef enum { t_invalid_token=0, t_ident,t_number, t_literal,
t_addop, t_mulop, t_lparen, t_rparen, t_less,
t_leq, t_neq, t_eql, t_geq, t_gt,
t_punctuation, t_keyword,t_dtyp, t_eol,t_skip } type_of_token;
char *tok_typ[]={"INVALID","IDENTIFIER","NUMBER","LITERAL",
"ADDOP", "MULOP", "LPAREN", "RPAREN", "LESS",
"LESSEQL", "NOTEQL", "EQUAL", "GREATEQ", "GREATER",
"PUNCT", "KEYWORD", "DTYP", "EOL", "skip"};
/* ================== Keywords ==================== */
char *keywords[] = { "LET", "FOR", "TO", "STEP", "NEXT", "DIM",
"IF", "THEN", "ELSE", "ENDIF", "READ",
"DATA", "END", "FUNCTION", "RETURN", "PROGRAM",
"INPUT", "PRINT", "INT", "LEFT$", "RIGHT$",
"MID$", "CHR", "ASC", "SUB", "RANDOMIZE",
"REM", "RND", "AS"};
#define keyword_count sizeof(keywords)/sizeof(char *)
char *type_specifiers[]={"BYTE","INTEGER","LONG","DOUBLE"};
#define dtype_count sizeof(type_specifiers)/sizeof(char *)
/* ========= Double linklist Implementation ====== */
// define a doubly linked list to represent a token.
typedef struct token
{
char value[50]; // string value
type_of_token t_type; // type
struct token *prev; // previous token to see previous
struct token *next; // next token
}token;
token *token_list = NULL; // list which represents the tokens of the program
// Function create_token takes a string and tokentype
// returns pointer to token
token *create_token(char *val, type_of_token t_typ)
{
token *t =NULL;
t=(token*)malloc(sizeof(struct token));
if(t == NULL)
{
printf("Error: Unable to allocate memory for token\n");
exit(-1);
}
bzero(t,sizeof(struct token));
strcpy(t->value,val);
t->t_type = t_typ;
t->prev = NULL;
t->next = NULL;
return(t);
}
// function push_back puts a token in token_list
// accepts a pointer to a token
// returns a pointer to a token
token *push_back(token *t)
{
token *temp = token_list;
// add at the end
if (token_list)
{
temp = token_list;
while(temp->next != NULL)
temp=temp->next;
temp->next=t;
t->prev = temp;
}
else
{
token_list = t;
}
return (token_list);
}
// Function push_front puts a token at the front of a list
// accepts a pointer to token
// returns a pointer to token
token *push_front(token *t)
{
if(token_list)
{
t->next=token_list;
token_list->prev = t;
t->prev = NULL;
token_list = t;
}
else
{
token_list = t;
}
return(token_list);
}
// Function display_list traverses the token_list and prints the token information
void display_list()
{
token *list;
list = token_list;
int line = 1;
//printf("DEBUG: token_list at %p\n",token_list);
while(list!=NULL)
{
printf("%s [ %s ]\n",list->value,tok_typ[list->t_type]);
if(list->t_type == t_eol)
line +=1;
list = list->next;
}
printf("Total Lines scanned = %d\n",line-1);
}
/* ================ lookahead ==================== */
// Function look look for the next character in the stream
// accepths a FILE pointer
// return a character
char look(FILE *source)
{
char ch='\0';
if(!feof(source))
{
ch = fgetc(source);
ungetc(ch,source);
}
return ch;
}
/* ============== sacnner Proper =================== */
// Function Parse, actual parser implementation actually it is lexer and parser combined
// accepts FILE pointer
// returns boolean value success or failure
bool scanner(FILE *source)
{
char peekc, inpc;
char tmpstr[50];
bzero(tmpstr,sizeof(tmpstr));
token *curr_token =NULL;
type_of_token t_typ = 0; //invalid token
while(!feof(source))
{
peekc= look(source);
//while(peekc == ' ' || peekc == 0x09)
// peekc = (char)fgetc(source);
inpc = (char)fgetc(source);
while(true)
{
if(isalpha(inpc)) /* symbol */
{
int i=0;
tmpstr[i]=inpc;
i++;
bool kw = false;
while(true)
{
peekc = look(source);
if(isalnum(peekc) || peekc == '_')
{
inpc = (char)fgetc(source);
tmpstr[i] = inpc;
i++;
}
else
{
tmpstr[i]='\0';
break;
}
}
for(i =0; i< keyword_count; i++)
{
if(strcasecmp(tmpstr,keywords[i])==0)
{
kw = true;
break;
}
}
if(kw == false)
{
t_typ = t_ident;
if(strcasecmp(tmpstr,"AS")==0)
t_typ = t_skip;
for(i=0;i')
{
t_typ = t_neq;
inpc = fgetc(source);
tmpstr[i] = inpc;
i++;
}
else if( peekc == '=')
{
t_typ = t_leq;
inpc = fgetc(source);
tmpstr[i] = inpc;
i++;
}
break;
case '>': t_typ = t_gt;
peekc = look(source);
if(peekc == '=')
{
t_typ = t_geq;
inpc = fgetc(source);
tmpstr[i] = inpc;
i++;
}
break;
case '+':
case '-': t_typ = t_addop;
break;
case '*':
case '/': t_typ = t_mulop;
break;
case '=': t_typ = t_eql;
break;
case '(': t_typ = t_lparen;
break;
}
break;
}
break;
}
if(t_typ != 0)
{
curr_token = create_token(tmpstr,t_typ);
token_list = push_back(curr_token);
bzero(tmpstr,sizeof(tmpstr));
curr_token = NULL;
t_typ = 0;
}
}
fclose(source);
return true;
}
void do_header(FILE *target)
{
if(target == NULL)
exit(-1);
fprintf(target,"#include \n");
fprintf(target,"#include \n");
fprintf(target,"#include \n");
fprintf(target,"#include
No comments:
Post a Comment