help with prog3_2.lua pls prog3_2.lua In Lua, implement the Infix to Postfix fun
ID: 3711766 • Letter: H
Question
help with prog3_2.lua pls
prog3_2.lua In Lua, implement the Infix to Postfix function that went over in class. The function
InfixToPostfix(str) takes a single argument (it will be an input string). To
kenize that input string
by space (split by space) and then apply the infix to postfix algorithm to return a postfix string.
prog3_3.cpp will be written in C/C++. It will be called prog3_3.cpp and can/should be an
extension of prog3_1.cpp The program will create a lua environment, load/run the file specified
by the command line argument. It will then take in a line of input from the stdin, call the
InfixToPostfix() function in lua (use the dostring), retrieve the resulting postfix string from the
lua stack (use the checkstring function) and then print the resultant postfix string.
this program is called prog3_3.cpp which complies but I am having trouble how to write a InfixToPostfix() function in lua help pls!
#include <lua.hpp>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
using namespace std;
int main(int argc, char * argv[]){
string buffer, infix;
const char *finalstring;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L,argv[1]);
getline(cin,buffer);
lua_getglobal(L,"InfixtoPostfix");
lua_pushstring(L,buffer.c_str());
finalstring = lua_tostring(L, -1);
cout << finalstring << endl;
lua_close(L);
return 0;
}
Explanation / Answer
--[[ Stack
A Simple Stack in Lua
Functions:
* push
* pop
* peek
* isEmpty
]]--
Stack = {}
Stack.__index = Stack
function Stack.new()
return setmetatable({}, Stack)
end
function Stack:push(input)
self[#self+1] = input
end
function Stack:pop()
if self:isEmpty() then
return -1
end
local tmp = self[#self]
self[#self] = nil
return tmp
end
function Stack:peek()
if self:isEmpty() then
return -1
end
return self[#self]
end
function Stack:isEmpty()
return #self == 0
end
function Stack:size()
return #self
end
--[[
--Testing the Stack
local s = Stack.new()
print(s:isEmpty()) -- {}
s:push(1) -- {1}
s:peek() -- {1}
print(s:isEmpty()) -- {1}
s:push(5) -- {1, 5}
s:push(10) -- {1, 5, 10}
print(s:pop()) -- {1, 5}
print(s:pop()) -- {1}
s:push(20) -- {1, 20}
print(s:pop()) -- {1}
print(s:pop()) -- {}
print(s:isEmpty()) -- {}
]]--
function prec(ch) --precedence
if ch == '+' or ch == '-' then
return 1
elseif ch == '*' or ch == '/' or ch == '%' then
return 2
elseif ch == '^' then
return 3
end
return -1
end
function infixToPostfix(input)
local result = ""
local oppStack = Stack.new()
--for token in string.gmatch(input, "%S+") do --gets rid of spaces
for i = 1, #input do
local token = input:sub(i,i)
--print("token: " .. token .. " precedence: " .. prec(token) .. " result: " .. result .. " stack size: " .. oppStack:size())
if prec(token) ~= -1 then --operator (+,-,*,/,%,^)
print("operator " .. token)
print(oppStack.size)
while not oppStack:isEmpty() and prec(token) <= prec(oppStack:peek()) do
print("poppin")
result = result .. oppStack:pop()
oppStack:push(token) --oops didnt have this
end
--[[
elseif token == '(' then
--print("start parentheses " .. token)
oppStack:push(token)
elseif token == ')' then
--print("end parentheses " .. token)
while not oppStack:isEmpty() and oppStack:peek() ~= '(' do
result = result .. oppStack:pop()
end
if not oppStack:isEmpty() and oppStack:peek() ~= '(' then
return "invalid parentheses"
else
oppStack:pop()
end
--]]
else --opperand (a,b,c,1,2,3)
--print("operand " .. token)
result = result .. token
end
end
while not oppStack:isEmpty() do
result = result .. oppStack:pop()
end
return result
end
function test()
print(infixToPostfix("a+b*(c^d-e)^(f+g*h)-i"))
--should print abcd^e-fgh*+^*+i-
end
test()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.