Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(S7): lexical analyzer #4

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.vscode
.vscode
a.out
**/**/a.out
**/a.out
23 changes: 23 additions & 0 deletions B-Tech-S7/WP/embed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>embed</title>
</head>
<body>
<script>
let n = parseFloat(prompt("Enter the number to find cube"))
if(!isNaN(n)){
result = document.createElement("div")
result.style.fontWeight = "400"
result.style.color = "green"
result.textContent = n * n * n
document.body.appendChild(result)
}
else {
alert("invalid input")
}
</script>
</body>
</html>
35 changes: 35 additions & 0 deletions B-Tech-S7/WP/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form</title>
</head>
<body>
<form action="https://www.mysite.com/reg.php" method="post">
<label for="textbox">Username</label>
<input type="text" name="textbox" maxlength="25">
<label for="radioBtn">Radio</label>
<div>
<label><input type="radio" name="radioBtn" value="radio1">Radio 1</label>
<label><input type="radio" name="radioBtn" value="radio2">Radio 2</label>
<label><input type="radio" name="radioBtn" value="radio3">Radio 3</label>
</div>
<label for="check">Checkbox</label>
<div>
<label><input type="checkbox" name="check1" value="check1">Check 1</label>
<label><input type="checkbox" name="check2" value="check2">Check 2</label>
<label><input type="checkbox" name="check3" value="check3">Check 3</label>
</div>
<label for="fruit">Select your favorite fruits:</label>
<select name="fruit" size="2">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
<option value="grape">Grape</option>
</select>
<button type="reset">Reset</button>
<button type="submit">Submit</button>
</form>
</body>
</html>
32 changes: 32 additions & 0 deletions B-Tech-S7/WP/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>web programming</title>
</head>
<body>
<form>
<input type="text" id="username">
<input type="password" id="password">
<input type="submit" value="login" onclick="return validate()">
</form>

<script>
function validate(){
console.log("test")
let username = document.getElementById("username").value
let password = document.getElementById("password").value
if(username.trim()===""){
alert("username cannot be blank")
return false
}
if(password.length < 6){
alert("Password must be atleast 6 characters")
return false
}
return true;
}
</script>
</body>
</html>
45 changes: 45 additions & 0 deletions B-Tech-S7/WP/table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Assignment</title>
</head>
<body>
<style>
table{
border: 1px dashed black 1px double black;
}
</style>
<table>
<tr>
<th rowspan="3">Food Item</th>
<th colspan="4">Recommended Intake</th>
</tr>
<tr>
<th colspan="2">age&lt;15</th>
<th colspan="2">age&gt;15</th>
</tr>
<tr>
<th>gm</th>
<th>Kcal</th>
<th>gm</th>
<th>Kcal</th>
</tr>
<tr>
<th>Cerials</th>
<td>1000</td>
<td>2000</td>
<td>750</td>
<td>1760</td>
</tr>
<tr>
<th>NonCerials</th>
<td>450</td>
<td>800</td>
<td>350</td>
<td>600</td>
</tr>
</table>
</body>
</html>
110 changes: 110 additions & 0 deletions B-Tech-S7/chatInter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <stdio.h>
#include <string.h>

int i = 0, j = 0, k = 0, t = 1;
char str[50], final[10], temp1[5], temp2[5], operator;
char stack[50]; // Stack to handle operator precedence and parentheses
int top = -1;

void push(char op) {
top++;
stack[top] = op;
}

char pop() {
if (top == -1) return '\0'; // Empty stack
char op = stack[top];
top--;
return op;
}

int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0; // for '('
}

void generateIntermediateCode(char op) {
char op2 = pop();
char op1 = pop();
printf("t%d = t%d %c t%d\n", t, t - 1, op2, t - 2);
t++;
push(op);
}

int main() {
puts("Enter the string");
gets(str);
while (str[i] != '\0' && str[i] != '=')
{
final[i] = str[i];
i++;
}
k = i + 1;
i = 0;
while (str[k] != '\0' && str[k] != '+' && str[k] != '-' && str[k] != '*' && str[k] != '/')
{
temp1[i] = str[k];
i++;
k++;
}
push('='); // Push '=' onto the stack to ensure proper precedence handling
operator = str[k];
k++;
i = 0;
while (str[k] != '\0')
{
for (j = 0; j < 10; j++)
{
temp2[j] = '\0';
}
if (str[k] == '(')
{
push('('); // Push '(' onto the stack
k++;
}
else if (str[k] == ')')
{
while (top != -1 && stack[top] != '(')
{
generateIntermediateCode(pop());
}
if (top == -1) {
printf("Invalid expression: Mismatched parentheses\n");
break; // Terminate the loop
}
pop(); // Pop '(' from the stack
k++;
}
else if (str[k] == '+' || str[k] == '-' || str[k] == '*' || str[k] == '/')
{
while (top != -1 && precedence(stack[top]) >= precedence(str[k]))
{
generateIntermediateCode(pop());
}
push(str[k]);
k++;
}
else
{
i = 0;
while (str[k] != '\0' && str[k] != '+' && str[k] != '-' && str[k] != '*' && str[k] != '/' && str[k] != ')')
{
temp2[i] = str[k];
i++;
k++;
}
printf("t%d = %s\n", t, temp2);
t++;
}
}
while (top != -1)
{
if (stack[top] == '(') {
printf("Invalid expression: Mismatched parentheses\n");
break;
}
generateIntermediateCode(pop());
}
return 0;
}
90 changes: 90 additions & 0 deletions B-Tech-S7/descentParser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include<stdio.h>
#include <ctype.h>

// Production rule
// E -> TE'
// E' -> +TE'|3
// T -> FT'
// T' -> *FT'|3
// F -> BF'
// F' -> /BF'|3
// B -> (E)|id

char input[100];
int i=0,error=0;

void E();
void EPrime();
void T();
void TPrime();
void F();
void FPrime();
void B();

void E() {
T();
EPrime();
}

void EPrime() {
if(input[i] == '+' | input[i] == '-'){
i++;
T();
EPrime();
}
}

void T() {
F();
TPrime();
}

void TPrime(){
if(input[i] == '*'){
i++;
F();
TPrime();
}
}

void FPrime(){
if(input[i] == '/'){
i++;
B();
FPrime();
}
}

void F() {
B();
FPrime();
}

void B() {
if(isalnum(input[i])){
i++;
}
else if(input[i] == '(') {
i++;
E();
if(input[i] == ')') {
i++;
}
else{
error++;
}
}
else {
error++;
}
}

void main() {
printf("Enter an arithmetic expression: ");
fgets(input, sizeof(input), stdin);
E();
if(error == 0)
printf("Approved...");
else
printf("Rejected...");
}
Loading