0% found this document useful (0 votes)
150 views

Dfa Simulation

This document defines a deterministic finite automaton (DFA) for lexical analysis. It includes: 1) Data structures to represent the DFA states, transitions between states based on input characters, and final accepting states. 2) Functions to define the DFA by inputting the number of states, accepting states, input characters, and transition table. 3) A function to simulate moving between DFA states based on character input.

Uploaded by

Mohsan Azad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

Dfa Simulation

This document defines a deterministic finite automaton (DFA) for lexical analysis. It includes: 1) Data structures to represent the DFA states, transitions between states based on input characters, and final accepting states. 2) Functions to define the DFA by inputting the number of states, accepting states, input characters, and transition table. 3) A function to simulate moving between DFA states based on character input.

Uploaded by

Mohsan Azad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include #include #include #include

<stdio.h> <stdlib.h> <string.h> <errno.h>

#define STATES 20 #define MAXTOKEN 10240 #define ERR -1 typedef struct tokenList_ { int kind, value; char *lexeme; struct tokenList_ *next; } tokenList; int transitions[STATES][256]; // transitions[i][j] gives the state after reading character j on state i enum {ID,LABEL,DOTWORD,INT,HEXINT,REGISTER,COMMA,LPAREN,RPAREN,WHITESPACE,ZERO,S TART,DOLLAR,MINUS,ZEROX,DOT,DOTW,DOTWO,DOTWOR,COMMENT} states; const int stateToKind[STATES] = {ID,LABEL,DOTWORD,INT,HEXINT,REGISTER,COMMA,LPAR EN,RPAREN,WHITESPACE,INT,ERR,ERR,ERR,ERR,ERR,ERR,ERR,ERR,ERR}; const char *kindToString[STATES] = {"ID","LABEL","DOTWORD","INT","HEXINT","REGIS TER","COMMA","LPAREN","RPAREN","WHITESPACE"}; const char *alphanumerals = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY Z0123456789"; const char *alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY Z"; const char *oneToNine = "123456789"; const char *digits = "0123456789"; const char *hexDigits = "0123456789abcdefABCDEF"; const char *whitespace = "\r\n\t "; void newLexer() { // Builds transition table to initiate lexer int i; const char *c; memset(transitions, ERR, sizeof(transitions)); for(c=alphabet; *c; c++) transitions[START][*c] = ID; ifiers for(c=alphanumerals; *c; c++) transitions[ID][*c] = ID; transitions[ID][':'] = LABEL; for(c=oneToNine; *c; c++) transitions[START][*c] = INT; s for(c=digits; *c; c++) transitions[INT][*c] = INT; transitions[START]['-'] = MINUS; for(c=digits; *c; c++) transitions[MINUS][*c] = INT; transitions[START]['0'] = ZERO; for(c=digits; *c; c++) transitions[ZERO][*c] = INT; transitions[ZERO]['x'] = ZEROX; for(c=hexDigits; *c; c++) transitions[ZEROX][*c] = HEXINT; for(c=hexDigits; *c; c++) transitions[HEXINT][*c] = HEXINT; transitions[START]['$'] = DOLLAR; ters for(c=digits; *c; c++) transitions[DOLLAR][*c] = REGISTER; for(c=digits; *c; c++) transitions[REGISTER][*c] = REGISTER; transitions[START][','] = COMMA; e character tokens

// ident

// value

// regis

// singl

transitions[START]['('] transitions[START][')'] transitions[START]['.'] directive transitions[DOT]['w'] transitions[DOTW]['o'] transitions[DOTWO]['r'] transitions[DOTWOR]['d'] transitions[START][';'] nts & whitespace for(i=0; i<256; i++) transitions[COMMENT]['\r'] transitions[COMMENT]['\n'] for(c=whitespace; *c; c++) }

= LPAREN; = RPAREN; = DOT; = = = = = DOTW; DOTWO; DOTWOR; DOTWORD; COMMENT;

// .word

// comme

transitions[COMMENT][i] = COMMENT; = WHITESPACE; = WHITESPACE; transitions[START][*c] = WHITESPACE;

void freeTokenList(tokenList *list) { if(list) { freeTokenList(list->next); free(list->lexeme); free(list); } } tokenList *newToken(int kind, const char *lexeme) { int value=0; if(kind == INT kind == HEXINT kind == REGISTER) { // Determine value if (lexeme[0]=='-') value = strtol(lexeme, NULL, 10); else if(lexeme[0]=='$') value = strtol(&lexeme[1], NULL, 10); else value = strtoul(lexeme, NULL, kind == HEXINT ? 1 6 : 10); if(errno == ERANGE) return NULL; // Out of range if(kind == REGISTER && value > 31) return NULL; // Out of range } tokenList *t = (tokenList *)malloc(sizeof(tokenList)); if(t == NULL) return NULL; // Memory error t->kind = kind, t->value = value, t->next = NULL; t->lexeme = (char *)malloc(sizeof(char)*(strlen(lexeme)+1)); if(t->lexeme == NULL) return free(t), NULL; // Memory error strcpy(t->lexeme, lexeme); return t; } char lexer_token[MAXTOKEN+1]; tokenList *scan(const char *input) { // O(N) Simplified Maximal Munch Algorithm int i=0, state, length; tokenList *head = NULL, **tail = &head; while(input[i]) { state = START, length = 0; while(transitions[state][input[i]] != ERR && length<MAXTOKEN) { // DFA u ntil error lexer_token[length++] = input[i]; state = transitions[state][input[i++]]; } lexer_token[length] = '\0'; if(stateToKind[state] == ERR) { // Not a final state fprintf(stderr, "ERROR stray character (%c) after (%.*s)\n", input[i ], i, input); return freeTokenList(head), NULL; }

if(state != WHITESPACE) { if((*tail = newToken(stateToKind[state], lexer_token)) == NULL) { fprintf(stderr, "ERROR out of range (%s) or insufficient memory\ n", lexer_token); return freeTokenList(head), NULL; } tail = &((*tail)->next); } } return head; } int main() { // Sample program demonstrating the use of the lexer char line[256]; newLexer(); tokenList *list, *head; while(fgets(line, sizeof(line), stdin)) { head = list = scan(line); while(list) { fprintf(stderr, " Token: %s {%s}\n", kindToString[list->kind], list ->lexeme); list = list->next; } freeTokenList(head); } return 0; }

#include<stdio.h> void DEFINE_DFA(); int MOVE_DFA(int,char); char letters[10]; int letter_count,count,final; int final_states[5]; int dfa[10][10]; main() { int s,i,accepted; char line[10]; printf("\t\t DFA SIMULATOR"); //Stores input letters //Stores set of final states

printf("\n\t******************************\n"); DEFINE_DFA(); do { s=0; i=0; accepted= 0; printf("\n\nEnter Input String.. "); scanf("%s",line); while(line[i]!='\0') if((s=MOVE_DFA(s,line[i++]))<0) break; for(i=0 ;i<final ;i++) if(final_states[i] ==s ) accepted= 1; (accepted)?printf("\a\n\tYES..!!!") : printf("\a\n\tNO...!!"); getch();

printf("\n\nDo you want to continue..??(y/n)"); } while(getch()=='y'); }

void DEFINE_DFA() { int i,j; char c; printf("Enter the no. of states.. "); scanf("%d",&count); printf("Enter the no. of input letters.. "); scanf("%d",&letter_count); printf("Enter the no. of final states.. "); scanf("%d",&final); for(i=0;i<final;i++) { printf("Final state %d : ",i+1); scanf("%d",&final_states[i]); } for(i=0;i<letter_count;i++) {

printf("\nLetter %d :",i+1); printf("%c",letters[i]=getch()); } printf("\n\nFill transition diagram.."); for(i=0;i<count;i++) { printf("\n\n\t _________\n\t STATE %d ",i); printf("\n\t __________\n"); for(j=0;j<letter_count;j++) { printf("\n\t %c \b\b\b",letters[j]); scanf("%d",&dfa[i][j]); } printf("\n\t ----------\n"); } } int MOVE_DFA(int s,char c) { int j; for(j=0;j<letter_count;j++) if(c==letters[j]) return(dfa[s][j]); return -1; }

You might also like